jQuery $.getJson 回调函数不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1740720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:04:37  来源:igfitidea点击:

$.getJson callback function is not working

jqueryajaxgetjson

提问by Santhosh S

I am calling a JSP by passing parameters which outputs a valid JSON as response, but still the $.getJsoncallback function is not getting fired. JSP page output is

我通过传递输出有效 JSON 作为响应的参数来调用 JSP,但仍然$.getJson没有触发回调函数。JSP页面输出是

 { "data": [ [ [ 1258185480000,4.39], 
               [ 1258186020000,4.31],
               [ 1258184940000,4.39],
               [ 1258183560000,4.39]  ] ] }

The URL points to the JSP page

URL 指向 JSP 页面

My jquerycode is

我的jquery代码是

<script id="source" language="javascript" type="text/javascript">
$(function () {   
  alert("before");
  $.getJson(URL,function(json){
            alert("hello");
          var plot = $.plot($("#placeholder"), json.data, options);
    });

 alert("after");
});

回答by Darin Dimitrov

The function is $.getJSONand not $.getJson

该函数是$.getJSON而不是$.getJson

回答by raghavsood33

$.getJSON( URL, function(data) {
  alert("hello");
});

is nothing but a shorthand for ajax call

只是 ajax 调用的简写

$.ajax({
  dataType: "json",
  url: URL,
  data: data,
  success: function(data) {
    alert("hello");
  }
});

BUT

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently ... For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes

重要提示:从 jQuery 1.4 开始,如果 JSON 文件包含语法错误,则请求通常会静默失败……例如,JSON 中表示的所有字符串,无论是属性还是值,都必须用双引号括起来

source: jquery.getjson docs

来源:jquery.getjson 文档

回答by cogline

I just spent about two hours on this. I found another post that discusses the difference between $.getJSONand $.getand how there really isn't any. So I swapped out my getJSON()for get()and it worked.

我只花了大约两个小时。我发现了另一篇文章,讨论的区别$.getJSON$.get如何真的没有任何。所以我换掉了我getJSON()get(),它奏效了。

(Also want to mention that I had also verified everything else was working by logging from the rails action and logging what I could from the javascript outsidethe callback function.)

(还想提一下,我还通过从 rails 操作中记录日志并从回调函数之外的javascript 中记录了我可以执行的操作来验证其他所有内容是否正常工作。)

回答by Mark B

$.getJSON will not utilize a callback with out a proper JSON object to process.

$.getJSON 不会使用没有合适的 JSON 对象来处理的回调。

回答by Andy Gaskell

Also ensure with Firebug that you are getting valid JSON back from the server.

还要确保使用 Firebug 从服务器获取有效的 JSON。

回答by Reimond Hill

For jQuery 3.4.1:

对于 jQuery 3.4.1:

$.getJSON("test.json", function (json) {
    console.log('Got JSON');
    console.log(json);

})
.fail(function (jqxhr, textStatus, error) {
    var err = textStatus + ", " + error;
    alert("There has been an error. If the problem persists contact the customer service");
})
.always(function () {
    console.log("complete");
});

If you believe that the JSON is ok and you are using chrome try "Empty Cache and Hard Reload".

如果您认为 JSON 没问题并且您正在使用 chrome,请尝试“清空缓存和硬重新加载”。