简体   繁体   English

为什么这个$ .getJSON的回调函数没有被执行?

[英]Why isn't this $.getJSON 's callback function being executed?

Here is the script that attempts to get the json file: 以下是尝试获取json文件的脚本:

jQuery(function($) {

    //////////////////////HEADLINE NEWS JSON SERVER START///////////////////////////
    var container = $("#headlineNews"); //cache the element
    console.log("First Log message is here!")
    $.getJSON("/JsonControl/Headline_News.json", function(jsonObj) {
        console.log("Second Log message is here!")
        var val = "";

        for (var i = 0; i < jsonObj.news.length; ++i) {
            val += "<div id='newsHeading'>" 
                + jsonObj.news[i].heading 
                + "</div><br/><div id='newsSummary'>" 
                + jsonObj.news[i].summary 
                + "</div><br/>";

            if (jsonObj.news[i].linkText != "" && jsonObj.news[i].linkPath != "") {
                val += "<a href='" + jsonObj.news[i].linkPath + "'>" + jsonObj.news[i].linkText + "</a><br/><br/>";
            }

            val += "<div class='entryDivider'>____________________________________________________</div>";
        }

        container.html(val);
    });

    //////////////////////HEADLINE NEWS JSON SERVER END/////////////////////////////
});​

Here is the json file itself: 这是json文件本身:

{
    "news": [
        {
        "heading": "Bulky Item Pick-Up to Begin May 4th, 2012 for Residential Utility Account Holders.",
        "summary": "Click on the link below for more details.",
        "linkText": "Bulky Item Pick-Up",
        "linkPath": "/Displayable Files/City_Bulk_Pick_Up_for_e_mailing.pdf"},
    {
        "heading": "NOW OPEN!",
        "summary": "OKMULGEE RECYCLING CENTER<br/>301 E. 3rd Street<br/>(Corner of E. 3rd St. and N. Muskogee Ave.).",
        "linkText": "WHAT TO AND WHAT NOT TO RECYCLE",
        "linkPath": "/Displayable Files/Recycling_Items.pdf"}
    ]
}​

//To omit any of these options, simply leave them blank (i.e., "linkText":"").

I have attempted to use console.log, but only the first one executes and the second one doesn't, so I know the contents of the $.getJSON branch isn't getting executed at all (meaning the $.getJSON statement is a fail, if I understand it correctly). 我试图使用console.log,但只有第一个执行而第二个没有,所以我知道$ .getJSON分支的内容根本没有执行(意味着$ .getJSON语句是一个失败,如果我理解正确的话)。 However absolutely no script errors occur. 但绝对不会发生脚本错误。

Also, the server IS set up to serve json files, as another tester site has executed an external json file just fine. 此外,服务器IS设置为提供json文件,因为另一个测试站点已经执行了外部json文件。

It feels like the path is wrong somehow, but I'm not getting a 404, and I've rechecked this path to make sure that it is syntactically correct at least a dozen times. 感觉路径在某种程度上是错误的,但我没有得到404,我已经重新检查了这条路径,以确保它在语法上至少正确十几次。

How can the $.getJSON command fail if the path to the file is correct, the syntax of the json file is correct, and the server definitely is configured to serve up json files (eg, application/json MIME type is set)? 如果文件的路径正确,json文件的语法是正确的,并且服务器肯定配置为提供json文件(例如,设置了application / json MIME类型),$ .getJSON命令怎么会失败? Is there anything else it could possibly be or would the second console.log not execute if the rest of the branch doesn't? 它还有什么可能的,或者如果分支的其余部分没有,第二个console.log是否会执行?

-------------------UPDATE----------------------------- ------------------- UPDATE -----------------------------

I have edited my post to reflect comments that I (erroneously) had in my json file. 我编辑了我的帖子以反映我(错误地)在我的json文件中的评论。

You should use $.ajax since it allows you to specify a success, error, and complete (finally) callback. 您应该使用$ .ajax,因为它允许您指定成功,错误和完成(最终)回调。 Perhaps your callback isn't being called because it's a success callback and the request is returning an error. 也许你的回调没有被调用,因为它是一个成功的回调,并且请求返回了一个错误。

$.ajax({
  url: 'ajax/test.html',
  type: 'POST',
  data: jsonData
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  },
  error: function(request, status, error) {
    //do stuff
  }
});

Try this and see if you get json returned in the console. 试试这个,看看你是否在控制台中返回了json。

$.getJSON("/JsonControl/Headline_News.json", function(jsonObj) {
   console.log(jsonObj);
});

JavaScript注释(或任何其他)在JSON语法中不合法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM