我有$ .getJSON调用,通过使一个Web API调用填充的集合:
$.getJSON("/api/rating", self.ratings);
我怎么会重写,如果我想添加一些在$中的选项。 ajax如beforeSend,数据,成功等?
编辑:我已经试过这两个和没有警报被击中:
$.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");
});
看到这个:http://stackoverflow.com/questions/1812585/beforesend-in-getjson – Jai 2013-02-13 18:02:29
如果你想使用beforeSend等人,你不能使用$ .getJSON,因为.getJSON只是一个简短的函数,调用$ .ajax函数。因此,请在下面查看我的答案 – Rickard 2013-02-14 08:10:04