2011-10-17 68 views
0

通过Ajax我收到来自PHP脚本编码的JSON字符串(json_encode):报价在jQuery.parseJSON

response = "{"type":"ok","mess":"File successfuly uploaded"}" 

,当我试图解析这个字符串jQuery.parseJSON(response); JS脚本失败堂妹在启动双引号并最终。

正常工作:

jQuery.parseJSON('{"type":"ok","mess":"File successfuly uploaded"}'); 

如何解决这个问题呢?

我总是收到语法错误: “UNEXPECTED_TOKEN”

解决:原因是不正确的文件编码。 UTF-8没问题

+0

试图逃逸引号` “{\” 类型\” ...` – 2011-10-17 12:19:45

回答

2

你已经解决了这个问题。您不能创建JSON字符串并忽略引用嵌套的标准规则。无论是其中的一个将工作:

//Denote string by using single quotes 
response = '{"type":"ok","mess":"File successfully uploaded"}' 

OR

//Continue to use double quotes and escape the inner quotes 
response = "{\"type\":\"ok\",\"mess\":\"File successfully uploaded\"}"; 
0

如何:

response = "{\"type\":\"ok\",\"mess\":\"File successfuly uploaded\"}"; 
response = "'" + response + "'"; 
jQuery.parseJSON(response);