在jQuery.parseJSON中引用

时间:2011-10-17 12:15:57

标签: javascript jquery ajax json

通过AJAX,我从php脚本中收到编码的json字符串(json_encode):

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

当我试图解析这个字符串jQuery.parseJSON(response);时,js-script在开始和结束时都失败了双引号。

工作正常:

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

如何解决这个问题?

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

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

2 个答案:

答案 0 :(得分: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\"}";

答案 1 :(得分:0)

这个怎么样:

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