【发布时间】:2013-02-14 10:07:29
【问题描述】:
我有一个 $.getJSON 调用,它通过调用 Web API 来填充集合:
$.getJSON("/api/rating", self.ratings);
如果我想添加 $.ajax 中的一些选项,例如 beforeSend、data、success 等,我该如何重写?
编辑:我已经尝试了这两种方法,但没有一个警报被击中:
$.getJSON("/api/rating")
.beforeSend(function (xhr) {
alert("before");
$('#divLoading').addClass('ajaxRefresh');
xhr.setRequestHeader('X-Client', 'jQuery');
})
.success(function (result) {
alert(result);
self.ratings = result;
})
.complete(function (result) {
alert("complete");
$('#divLoading').removeClass('ajaxRefresh');;
})
.error(function () {
alert("error");
});
$.getJSON("/api/rating", self.ratings)
.beforeSend(function (xhr) {
alert("before");
$('#divLoading').addClass('ajaxRefresh');
xhr.setRequestHeader('X-Client', 'jQuery');
})
.success(function (result) {
alert(result);
self.ratings = result;
})
.complete(function (result) {
alert("complete");
$('#divLoading').removeClass('ajaxRefresh');;
})
.error(function () {
alert("error");
});
【问题讨论】:
-
如果要使用 beforeSend 等,则不能使用 $.getJSON,因为 .getJSON 只是调用 $.ajax 函数的简写函数。因此,请参阅下面的答案
标签: javascript jquery asp.net-web-api