0

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

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

URL 指向 JSP 页面

我的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");
});
4

6 回答 6

10

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

于 2009-11-16T08:04:35.087 回答
10
$.getJSON( URL, function(data) {
  alert("hello");
});

只是 ajax 调用的简写

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

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

来源:jquery.getjson 文档

于 2015-09-21T08:05:40.377 回答
3

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

于 2012-12-09T19:24:49.560 回答
2

我只花了大约两个小时。我发现另一篇文章讨论了两者之间的区别$.getJSON以及$.get实际上没有区别。所以我换掉了我getJSON()get(),它起作用了。

(还想提一下,我还通过从 rails 操作记录并从回调函数外部的javascript 记录我可以记录的内容,验证了其他一切是否正常。)

于 2011-08-04T14:50:55.403 回答
1

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

于 2009-11-16T08:06:06.247 回答
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");
});

如果您认为 JSON 没有问题并且您正在使用 chrome,请尝试“Empty Cache and Hard Reload”。

于 2019-07-24T09:04:50.910 回答