2013-04-25 86 views
1

我有一个链接,点击哪个ajax POST请求被激活。 我这样做:如何通过点击链接访问ajax请求的响应?

$('a#create_object').click(); 

这触发一个Ajax请求。这个代码($.ajax({...}))写在bootstrap库中的某处,我不想编辑这个thwem。

如何在ajax成功后访问请求的响应?

+0

向我们展示您的代码 – 2013-04-25 13:00:23

回答

0
$('#controlId').click(function(){ $.ajax({ 
     type: "POST", 
     url: "PageUrl/Function", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 
     alert(response.d); //here you can get the response on success of function 
     } 
    }); 
}); 
1

直接回调以来的jQuery 1.8.0已被弃用,但你可以用中,.done,.fail和。总是回调!

如果你想做一些东西,你必须覆盖/访问回调,你不能从外部访问它我的意思是!

$('#link').on('click', function(e) { 
    $.ajax({ 
     type: "post", 
     url: "your-page", 
     data: {}, 
     dataType: "json" 
    }).done(function (response) { 
     alert('Success'); 
     console.log(response); // Use it here 
    }).fail(function (response) { 
     alert('Fail'); 
     console.log(response); // Use it here 
    }).always(function (response) { 
     // Here manage something every-time, when it's done OR failed. 
    }); 
});