Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $.getJSON语法_Javascript_Jquery_Asp.net Web Api - Fatal编程技术网

Javascript $.getJSON语法

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调用,它通过进行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");
          });

$.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 () {
  });