getJSON回调函数未执行

时间:2014-02-04 08:26:57

标签: javascript jquery json

我在名为reqfiled.json

的文件中使用了JSON
{
"Activity List View" :
    {
    "Activity Form Applet":{
        "Asset Number": "Y",
        "Type":"Y",
        "Comment":"Y",
        "Status":"Y",
        "Priority":"Y"
        }
    }
}

我有以下jQuery代码

$.getJSON("23030/scripts/siebel/custom/reqfield.json", function(data) {
                console.log("hi");
            });

但是回调函数没有执行。我已经检查了firebug和chrome net标签,调用成功完成,并在回复时返回了json数据。

还有什么问题可以解决?如何进行调试?

1 个答案:

答案 0 :(得分:1)

您可能使用了错误的网址/路径,但为了确保在特定情况下添加一些要回调的回调:

$.getJSON("23030/scripts/siebel/custom/reqfield.json", function(data)
{
    console.log(data);
}).fail(function()
{
    console.log('error');
    console.log(arguments);//<-- check these for clues
}).always(function()
{//add this to check if your code even gets executed...
    console.log('getJSON called');
});

我还会在代码中的console.log(data)行(console.log('hi');上设置一个断点,只是因为 if 你说文件确实已发送,然后我我想知道哪里出错了。确保json文件只包含 JSON数据,并仔细检查控制台,看看之后可能出现的任何JS警告/错误 getJSON电话。
我还没有检查jQ如何处理这些json文件,但我不是100%肯定你的格式。最好在控制台中写出文字,然后将JSON.stringify的输出复制粘贴到该文件中:)