当PHP提供的JSON响应包含单引号或撇号时,如何避免parseerror?
假设我有一个返回json响应的PHP脚本。它看起来像这样:当PHP提供的JSON响应包含单引号或撇号时,如何避免parseerror?,php,ajax,json,jquery,Php,Ajax,Json,Jquery,假设我有一个返回json响应的PHP脚本。它看起来像这样: $ctype = ContentNego::desiredContentType(); header("Content-Type: $ctype"); if (ContentNego::flavor($ctype) == ContentFlavor::JSON) { echo '{ "name": "' . $name . '", ' . '"comment": "' . $comment . '"}'; }
$ctype = ContentNego::desiredContentType();
header("Content-Type: $ctype");
if (ContentNego::flavor($ctype) == ContentFlavor::JSON) {
echo '{ "name": "' . $name . '", ' .
'"comment": "' . $comment . '"}';
}
.... other content types here....
在上面的代码之前,我已经
class ContentFlavor {
const XML = 1;
const JSON = 2;
const TEXT = 3;
const Unknown = 4;
}
而$cftype
的值是通过检查传入的Accept头来设置的,这是内容类型协商的基本形式
php脚本回显的字符串如下所示:
[ "name" : "Vettel", "comment" : "He's terrific" ]
$.ajax({type: "GET",
url: url,
dataType: "json",
headers : { "Accept" : 'application/json' },
cache: false,
error: function (xhr, textStatus, errorThrown) {
enableEditboxButtons();
},
success: function (data, textStatus, xhr) {
// fill and show the editbox dialog
...
}});
在浏览器中,我有如下javascript:
[ "name" : "Vettel", "comment" : "He's terrific" ]
$.ajax({type: "GET",
url: url,
dataType: "json",
headers : { "Accept" : 'application/json' },
cache: false,
error: function (xhr, textStatus, errorThrown) {
enableEditboxButtons();
},
success: function (data, textStatus, xhr) {
// fill and show the editbox dialog
...
}});
但当我在jQuery端收到这些内容时,我得到了以下信息:
[ "name" : "Vettel", "comment" : "He\'s terrific" ]
注意注入的反斜杠“转义”撇号或单引号。我的代码没有把反斜杠放在那里。它似乎是由PHP注入的?此外,它似乎违反了以下条款中规定的JSON编码要求:
在jQuery 1.7.1中,带有转义撇号的json字符串无法解析,并且调用了我的错误fn
怎么回事?PHP真的在做这种转义吗?如果是,为什么/如何做?(它在Linux上的PHP5.2.17上运行)我可以避免吗
如果没有,我如何解决这个问题 我搜索并找到了
我把这段代码放在我的浏览器端脚本中,它在文档就绪时运行一次
$.ajaxSetup({
// Apparently, when sending application/json, if there is a
// single-quote contained in a string value, PHP erroneously escapes
// it. This causes jQuery to barf with a parsererror.
//
// To work around the problem, use a custom converter to fixup
// that invalid json serialization.
//
// e.g. { "comment": "Grandma\'s sweater" } -->
// { "comment": "Grandma's sweater" }
converters: {
"text json": function (textValue) {
return jQuery.parseJSON(textValue.replace(/(^|[^\\])\\'/g, "$1'"));
}
}
});
我仍然不清楚撇号是如何或为什么会出现在那里的,但当浏览器接收到JSON时,它就会被去掉 我搜索并找到了
我把这段代码放在我的浏览器端脚本中,它在文档就绪时运行一次
$.ajaxSetup({
// Apparently, when sending application/json, if there is a
// single-quote contained in a string value, PHP erroneously escapes
// it. This causes jQuery to barf with a parsererror.
//
// To work around the problem, use a custom converter to fixup
// that invalid json serialization.
//
// e.g. { "comment": "Grandma\'s sweater" } -->
// { "comment": "Grandma's sweater" }
converters: {
"text json": function (textValue) {
return jQuery.parseJSON(textValue.replace(/(^|[^\\])\\'/g, "$1'"));
}
}
});
我仍然不清楚撇号是如何或为什么会出现在那里的,但当浏览器接收到JSON时,它就会被去掉 Cheeso,确保在PHP配置中关闭了*magic\u quotes\u gpc*和*magic\u quotes\u runtime*选项(这可能会将反斜杠放在那里,更多信息)
此外,如果可以的话,您可能应该使用该方法,因为您可以直接将数组或对象传递给它,并获取所表示数据的JSON版本,这会使您的工作变得更加轻松。Cheeso,请确保在PHP配置中关闭了*magic\u quotes\u gpc*和*magic\u quotes\u runtime*选项(这可能是把反斜杠放在那里,更多信息)
此外,如果可以的话,您可能应该使用该方法,因为您可以直接将数组或对象传递给它,并获得表示数据的JSON版本,这会使您的工作变得更轻松。不要在意这个答案。Nathan的答案更好。不要在意这个答案。Nathan的答案更好。