Javascript $.ajax在有效JSON上抛出解析错误
我遇到了一个小问题。我试图使用jQueryJavascript $.ajax在有效JSON上抛出解析错误,javascript,json,jquery,Javascript,Json,Jquery,我遇到了一个小问题。我试图使用jQuery$访问JSON文件。ajax调用我在网上进行了双重检查,我的JSON代码是有效的。当我调用时,它抛出一个解析错误,JSON中有语法错误 您可以通过访问找到错误 以下是我的javascript: $(document).ready(function() { $.ajax( { type: "GET", url: 'projects/projects.json', dataType: "jso
$访问JSON文件。ajax
调用我在网上进行了双重检查,我的JSON代码是有效的。当我调用时,它抛出一个解析错误,JSON中有语法错误
您可以通过访问找到错误
以下是我的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"
}
]
}
记录错误听起来像是json的问题。尝试在json中切换
[]
以获得{}
我已经测试了你的代码及其对我的作用,我也在你的网站上试用过,并给出了304未修改
因此,请尝试使用getJson()
jQuery方法
$.getJSON('projects/projects.json', function(data) {
});
我测试时,你的代码运行良好。根据语法错误是什么,我可能会检查json是如何生成的,以及导航到projects/projects.json是否正确生成json。如果生成了json,试着用静态json替换它,看看它是否有效 在Chrome中工作得很好,即对JSON的要求特别严格
注意第二和第三个描述中的新行。我怀疑这就是IE的失败之处。错误与第二个数组条目中两段之间的空格(可能是换行符)有关
...in the game.</p>
<p>Gridlock...
^
|
the problem
…在游戏中
交通堵塞。。。
^
|
问题
JSON中有制表符和换行符,这会导致解析器失败。用\t
替换每个选项卡实例,用\n
替换换行符,例如:
示例JSON文件:
{
textWithTabs : "This is text with \t a tab and \n newline"
}
var o = JSON.parse(json);
console.log(o.textWithTabs); //This is text with a tab and ↵ newline
JavaScript:
{
textWithTabs : "This is text with \t a tab and \n newline"
}
var o = JSON.parse(json);
console.log(o.textWithTabs); //This is text with a tab and ↵ newline
我建议你试试这个:
{
"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"
}
]
}
{
“项目”:[
{
“标题”:“Adobe套件”,
“说明”:“凭借多年使用Adobe Suite产品的经验,我掌握的技能包括在Adobe Illustrator中创建矢量艺术品、在Adobe Photoshop中安排web版面以及在Adobe InDesign中创建打印版面”,
“imgsrc”:“img/adobe_suite_description.png”
},
{
“标题”:“僵局”,
“描述”:“在我大学二年级的时候,我和杰森·菲奇(Jayson Fitch)合作制作了一个名为Gridlock的等距2d射击游戏。我们作为一个团队创造了游戏中使用的所有艺术资产。Gridlock仍在开发中,我们希望在OUYA上发布它。请查看这里的开发博客。”,
“imgsrc”:“img/gridlock_description.png”
},
{
“标题”:“开放式购物车”,
“描述”:“作为一个自由职业者,我发起了对传奇王国地形电子商务店面的彻底改造。这需要一个完整的视觉重新品牌化,以及创建一个后端解决方案来扩展在线支付选项。请访问传奇王国地形”,
“imgsrc”:“img/opencart_description.png”
}
]
}
您是否使用Fiddler()确保服务器正在发送您期望的内容?您能显示错误吗?就像麦克说的,试试看,这样你可以复制它,我们可以看到它。对我来说很好。哇,对不起,麦克,完全不同。我必须自己检查一下。@John我用一个链接更新了我的问题,这个链接指向了问题发生的网站。我已经尝试过了,并且按照预期工作……而且,你上面问题中的JSON并不是响应中实际出现的内容。问题是项目应该是一个数组。另外,将其设置为{}
将使JSON文件无效。当我使用$.getJSON()
时,它看起来不像是在运行回调。当我看着fiddler时,它仍然在抛出一个304,看看我的答案。只需将选项卡替换为\t
,将换行符替换为\n
,这肯定是导致问题的原因。谢谢