2013-04-25 84 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. 
    }); 
});