Json文件不加载或显示警报 [英] Json file not loading or showing alerts
问题描述
这似乎是一个常见问题,但我不知道我的生活在哪里,我已经验证了我的json文件,并且没有错误,我也可以发誓说,everythng正在完美地工作在一个点。 im在一个文件夹中本地加载所有内容以下是我的jquery文件加载我的json文件
pre $ $(document).ready(function (){
//载入json文件
var catelogue;
var url =shoppingItems.json;
$ .getJSON(url,函数(json){
alert(json.shoppingItem [0] .name);
});
});
这就是我的json文件的外观,并且是我的 http://jsonlint.com/ 进行验证
{
shoppingItem:[
{
name:LG Tone,
model_no:HBS-730,
type: 蓝牙立体声,
价格: 45.00,
库存:10,
images:images / Headphones / lgtoneshbs.jpg
} ,
{
name:NoiseHush,
model_no:NX22,
type:有线耳机,
价格 : 69.95,
库存: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:蓝牙立体声,
价格:£35.00,
库存:10,
images:images / Headphones / earpnoisehush。 jpg
}
]
}
即使我试图提醒json文件
我已将您的解决方案复制到我的机器上,并且工作得很好。你只需要通过http://而不是file:///来运行脚本。
This is a common question it seems but i cant for the life of me find out where exactly im goin wrong, i have validated my json file and there are no errors, i could also swear tha t everythng was working perfectly at one point. im loading everything locally within one folder the following is my jquery file to load my json file
$(document).ready(function() {
// loading json file
var catelogue;
var url = "shoppingItems.json";
$.getJSON(url, function(json) {
alert(json.shoppingItem[0].name);
});
});
This is what my json file looks like and yes ive gone to http://jsonlint.com/ to validate
{
"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"
}
]
}
Nothing gets allerted, even if i try to alert the json file
I've replicated your solution on my machine and it works just fine. You just have to run your script over http:// instead of file:///.
这篇关于Json文件不加载或显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!