$. getjson()在IE9中工作,但不在Chrome和FireFox中,我的代码有什么问题

$.getJSON() works in IE9, but not in Chrome and FireFox, whats wrong in my code?

本文关键字:我的 FireFox 代码 问题 什么 Chrome getjson IE9 工作      更新时间:2023-09-26

我使用IE9和一个文本编辑器开发了一个web应用程序。它读取一个JSON文件,然后根据该文件以及JavaScript和jQuery代码的逻辑填充一些DIV元素。在IE9下,它运行得很好。

在Chrome下,它无法执行$. getjson()语句,因此没有数据可用。在FireFox下,$. getjson()语句显然运行(警报消息证明了这一点),但它不读取任何内容。

JSON文件传递JSONLint。

Chrome和FireFox都没有显示任何错误。

我使用JSON站点的JSON数据制作了一个示例文件,通过JSONLint验证它,然后使用该文件运行我的代码。没有区别——Chrome仍然会忽略$. getjson()语句。

我的代码的相关部分:

    function buildTree(centralID) {
      alert("Can we start, at least?");
     $.getJSON('sample.json', function(data) {
      alert("first success");
      $.each(data.person, function(i, xdata) {

Chrome显示第一个警报,但不显示第二个。

任何想法?

您可以使用内置的error函数来显示错误并进行调试。

$(document).ready(function(){ // Make sure your jQuery is inside this
$.getJSON("sample.json", function(data) {
    alert('Point 1 Reached.');
    console.log(data); // Here we log to our console
    $.each(data.feed.entry, function(i, item) {
             // Do your stuff here
        }); // End of $.each
// Here Success, Completed & Error do something. chained onto $.getJSON
        }).success(function() { alert("success"); })
          .error(function(jqXHR, textStatus, errorThrown) { // Debug the error!!
                    console.log("error " + textStatus);
                    console.log("error throw " + errorThrown);
                    console.log("incoming Text " + jqXHR.responseText);
                }) // End of .error
          .complete(function() { alert("complete"); });
        });
}); // End of DOM Ready

这应该会在firefox或chrome的控制台窗口显示错误(console.log在IE中不起作用,会破坏脚本)。要在firefox或chrome中打开控制台窗口,请按F12。如果JSON不工作,它会显示一个错误,你可以调试。



还要确保此代码在您的$(document).ready()中。$. getjson()如果以其他方式使用,可能会导致跨浏览器的意外行为。

这是在web服务器上运行还是您只是在浏览器中打开文件?如果你只是打开文件,你会有问题