Javascript $.getJSON语法
我有一个$.getJSON调用,它通过进行Web API调用来填充集合:Javascript $.getJSON语法,javascript,jquery,asp.net-web-api,Javascript,Jquery,Asp.net Web Api,我有一个$.getJSON调用,它通过进行Web API调用来填充集合: $.getJSON("/api/rating", self.ratings); 如果我想添加$.ajax中的一些选项,如beforeSend、data、success等,我该如何重写 编辑:我已尝试了这两种方法,但未命中任何警报: $.getJSON("/api/rating") .beforeSend(function (xhr) { aler
$.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");
});
$.getJSON是以下内容的简写:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
因此,您的示例可以转化为:
$.ajax({
dataType: "json",
url: "/api/rating",
data: self.ratings,
beforeSend: beforeSend,
success: function (json) {
// handle json
}
});
等等。$.getJSON(,)
.success(函数(){
})
.错误(函数(){
});
编辑:
.success()
和.error()
已被弃用,因此应改用.done()
和.fail()
。请看这一点:如果要使用beforeSend等,则不能使用$.getJSON,因为.getJSON只是一个调用$.ajax函数的简单函数。因此,请参见下面我的答案。self.ratings会在这里出现在哪里?@user517406什么是self.ratings
?此时,它可能是数据,也可能是一个成功处理程序..success和.error被贬低为.done和.fail
$.getJSON(<url>,<data>,<callback>)
.success(function() {
})
.error(function () {
});