リンクをクリックすると、ajax POST リクエストが起動されます。私はこれをします:
$('a#create_object').click();
これにより、ajax リクエストが発生します。この ( $.ajax({...})
) のコードは、ブートストラップ ライブラリのどこかに書かれており、編集したくありません。
ajaxが成功した後、リクエストのレスポンスにアクセスするにはどうすればよいですか?
リンクをクリックすると、ajax POST リクエストが起動されます。私はこれをします:
$('a#create_object').click();
これにより、ajax リクエストが発生します。この ( $.ajax({...})
) のコードは、ブートストラップ ライブラリのどこかに書かれており、編集したくありません。
ajaxが成功した後、リクエストのレスポンスにアクセスするにはどうすればよいですか?
ダイレクト コールバックは jQuery 1.8.0 以降非推奨ですが、.done、.fail、および .always コールバックを使用できます。
そして、何かをしたい場合は、コールバックを上書き/アクセスする必要があります。つまり、外部からアクセスすることはできません!
$('#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.
});
});
$('#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
}
});
});