IE8上的json解析失败 [英] json parse fails on IE8

查看:162
本文介绍了IE8上的json解析失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

json->返回的响应

{"msg":"<DIV><P>Unfortunately we were unable to process your application.</p><BR>Please upload a CV<BR></DIV>", "status":"error"} 

错误

SCRIPT1015: Unterminated string constant

出现错误的行

var data = $(this).contents().find('body').html();//I have console logged this and holds the above json as a string.
data = jQuery.parseJSON(data);// problem is here.

出于某种原因,我认为这可能是因为.text().html()相反而导致的html标记,但是我需要使用.html(),因为它是使用html标记格式化的.

for some reason I think, it may be because the html tags because .text() works as oppose to .html(), however I need to use the .html() because it's formatted there with html tags.

有什么建议吗?

推荐答案

json中是否有换行符?如果是这样,那就是您的错误.尝试使用data.replace( '\n', ' ' )之类的方法消除它.

Is that a line break in the json? If that's the case, then that's your error. Try eliminating it with something like data.replace( '\n', ' ' ).

评论更新:Val意识到IE8解析器实际上在他的测试用例上令人厌烦,而原始的JavaScript JSON解析器处理得很好.如果您需要该原始解析器,则位于: https://github.com/douglascrockford/JSON-js链接到以下来源: http://www.json.org ,替换的内容几乎减少了.

Update from the comments: Val realized that the IE8 parser was in fact borking on his test case, while the original javascript JSON parser handled it just fine. If you need that original parser, it's at: https://github.com/douglascrockford/JSON-js as linked to from: http://www.json.org and is pretty much a drop in replacement.

这篇关于IE8上的json解析失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆