我遇到了一个小问题。我试图使用jQuery $.ajax
调用来访问JSON文件我在线检查了两次并且我的JSON代码是有效的。当我进行调用时,它会在JSON中抛出语法错误的解析错误。
您可以访问http://michael-nolan.com/
找到错误这是我的javascript:
$(document).ready(function()
{
$.ajax(
{ type: "GET",
url: 'projects/projects.json',
dataType: "json",
success: function(results)
{
console.log("Success!");
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
console.log(textStatus); console.log(errorThrown);
}
});
});
这是我的JSON
{
"projects":
[
{
"title":"Adobe Suite",
"description":"Some stuff",
"imgsrc":"img/adobe_suite_description.png"
},
{
"title":"Gridlock",
"description":"Stuff",
"imgsrc":"img/gridlock_description.png"
},
{
"title":"Open Cart",
"description":"more stuff",
"imgsrc":"img/opencart_description.png"
}
]
}
答案 0 :(得分:2)
在Chrome中运行良好,IE对JSON(对所有事情)都特别严格。
请注意第2和第3个描述中的换行符。我怀疑这是IE中失败的原因。
答案 1 :(得分:1)
错误与第二个数组条目中两段之间的空格(可能是换行符)有关
...in the game.</p>
<p>Gridlock...
^
|
the problem
答案 2 :(得分:0)
我已经测试了您的代码及其为我工作,我也在您的网站上尝试了它并给了304 not modified
。
所以尝试使用getJson()
jQuery方法。
$.getJSON('projects/projects.json', function(data) {
});
答案 3 :(得分:0)
我测试时你的代码运行正常。根据语法错误的原因,我可能会检查如何生成json以及导航到projects / projects.json是否正确生成了json。如果生成了json,请尝试用静态json替换它,看看它是否有效。
答案 4 :(得分:0)
JSON中有选项卡和换行符,导致解析器失败。将每个标签实例替换为\t
,将换行符替换为\n
例如:
示例JSON文件:
{
textWithTabs : "This is text with \t a tab and \n newline"
}
<强> JavaScript的:强>
var o = JSON.parse(json);
console.log(o.textWithTabs); //This is text with a tab and ↵ newline
答案 5 :(得分:0)
我建议你试试这个:
{
"projects": [
{
"title": "Adobe Suite",
"description": "<p>With years of experience using the Adobe Suite products I have skills that range from creating vector artwork in Adobe Illustrator, arranging web layouts in Adobe Photoshop, and creating print layouts in Adobe InDesign</p>",
"imgsrc": "img/adobe_suite_description.png"
},
{
"title": "Gridlock",
"description": "<p>In my Sophomore year in college I worked with Jayson Fitch as an artist on a isometric 2d shooter called Gridlock. We as a team created all of the art assets that are being used in the game.</p><p> Gridlock is still in development and we hope to release it on the OUYA.Check out it's development blog here. <a href='http://gridlock-game.tumblr.com'>www.Gridlock-Game.Tumblr.com</a></p>",
"imgsrc": "img/gridlock_description.png"
},
{
"title": "Open Cart",
"description": "<p>As a freelance project I initiated an overhaul of the Legendary Realms Terrain e-commerce storefront. This entailed a complete visual re-branding as well as creating a backend solution to expand online payment options.</p><p> Feel free to check them out at < a href = 'lrterrain.com' > Legendary Realms Terrain < /a></p > ",
"imgsrc": "img/opencart_description.png"
}
]
}
答案 6 :(得分:-1)
记录的错误听起来像是你的json问题。尝试在json中切换[ ]
{}
。