【问题标题】:Json file not loading or showing alertsJson 文件未加载或显示警报
【发布时间】:2013-04-03 22:04:17
【问题描述】:

这似乎是一个常见问题,但我一生都无法找出我到底哪里出错了,我已经验证了我的 json 文件并且没有错误,我也可以发誓一切都在一个完美的工作观点。我在一个文件夹中本地加载所有内容以下是我的 jquery 文件来加载我的 json 文件

$(document).ready(function() {

    // loading json file
    var catelogue;
    var url = "shoppingItems.json";

    $.getJSON(url, function(json) {
        alert(json.shoppingItem[0].name);
    });
});

这就是我的 json 文件的样子,是的,我去了http://jsonlint.com/ 进行验证

{
    "shoppingItem": [
        {
            "name": "LG Tone",
            "model_no": "HBS-730",
            "type": "Bluetooth Stereo",
            "price": "�45.00",
            "stock": 10,
            "images": "images/Headphones/lgtoneshbs.jpg"
        },
        {
            "name": "NoiseHush",
            "model_no": "NX22",
            "type": "Wired Headphones",
            "price": "�69.95",
            "stock": 10,
            "images": "images/Headphones/noisehushhead.jpg"
        },
        {
            "name": "NoiseHush",
            "model_no": "NX80",
            "type": "Earphones",
            "price": "�25.00",
            "stock": 10,
            "images": "images/Headphones/earpnoisehush.jpg"
        },
        {
            "name": "Motorola MOTOROKR",
            "model_no": "S305",
            "type": "Bluetooth Stereo",
            "price": "£35.00",
            "stock": 10,
            "images": "images/Headphones/earpnoisehush.jpg"
        }
    ]
}

即使我尝试提醒 json 文件,也没有任何东西被提醒

【问题讨论】:

  • 在您的 getJSON 成功回调中尝试 console.log(json),并通过检查 firebug 控制台确保您按预期获取数据
  • 您的代码是否从本地file:/// URL 运行? AJAX 在本地文件的浏览器中不能完全一致地工作,在大多数情况下它根本不起作用。
  • 如果你只提醒 json 而不是 json 中的项目会发生什么?
  • 这可能与问题无关,但看到 而不是 £ 表明有一些东西需要修复。
  • 它对我来说很好jsfiddle.net/YUuzG

标签: jquery html json


【解决方案1】:

我已经在我的机器上复制了您的解决方案,并且效果很好。您只需通过 http:// 而不是 file:/// 运行脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    相关资源
    最近更新 更多