以下のように、すべての Ajax 呼び出しに共通の機能を記述しようとしています。下のように分けて書くとアボートがうまくいきませんが、同じ ajax に beforesend を追加するとうまくいきます。
$(document).ready(function () {
$.ajax({
beforeSend: function (jqXHR, settings) {
check = TestCallMethod();
if (check.responseText != "true") {
alert("Call Failed");
jqXHR.abort(event);
}
}
});
$.ajax({
// beforeSend: function (jqXHR, settings) {
// debugger;
// check = TestCallMethod();
// if (check.responseText != "true") {
// alert("Call Failed");
// jqXHR.abort();
// }
// },
cache: false,
url: '@Url.Action("TestCall2", "Home")',
success: function (data) {
alert(data);
},
error: function (request, status, error) {
alert(request.responseText);
}
});
});
function TestCallMethod()
{
return $.ajax({
url: '@Url.Action("TestCall", "Home")',
async: false,
cache: false,
success: function (data) {
return data;
},
error: function (request, status, error) {
alert(request.responseText);
return false;
}
});
}
一般的なajax beforsendの中止を達成するには?