Jquery.Ajax 在我的 JSON 回复中添加双引号和新行
Jquery.Ajax adds double quotes and new line to my JSON response
我最近将我的开发服务器从 PHP 5.4 升级到 PHP 5.6,我在尝试测试它时遇到了一个奇怪的错误。我的所有 PHP 代码 returns 数据通过我的主视图作为数组或字符串,它使用以下代码将数据作为 JSON 发送回任何 Ajax 查询。 (出于测试目的,我停用了 MIME 类型)
public static function SendData($data)
{
$response = array(
'status' => true,
'message' => $data
);
//header('Content-type: application/json');
exit(json_encode($response, JSON_HEX_APOS));
}
据我的 Javascript 代码使用该数据,通常立即将其解析为 JSON,因为 MIME 类型告诉 JQuery 这样做并处理它。它在 PHP 5.4 中运行良好,但自从我升级后,我的响应被 Jquery 修改了。查看服务器在 FireBug 中的响应,我发现它是有效的 JSON(使用 JSONlint 测试)
{"status":true,"message":"Didier Tartempion|dtartempion@example.com|Gestion de commerce"}
但是当我检查 firebug
中的以下 Jquery 代码时
$.ajax({
url:"Connect.php",
type : 'POST',
data : data,
success:function(result)
{
//JSON processing, for now I debugg with
alert (result);
},
error: function(qXHR, textStatus, errorThrown)
{
alert(textStatus);
}
});
success 中的结果变量实际上包含我的格式奇怪的响应,使用 firebug watch 时看起来像这样:
"\n{"status":true,"message":"Didier Tartempion|dtartempion@example.com|Gestion de commerce"}"
因此,JQuery 无法解析它并且 JSONlint 说它无效。 PHP5.6 甚至可能是新服务器的某些东西可能会导致这种奇怪的行为?
谢谢
只是回应数据
echo json_encode($response, JSON_HEX_APOS);
我建议你使用 return
而不是 exit
public static function SendData($data)
{
$response = array(
'status' => true,
'message' => $data
);
//header('Content-type: application/json');
return json_encode($response, JSON_HEX_APOS);
}
在您的 ajax 调用中添加 dataType
并添加 console.log()
$.ajax({
url:"Connect.php",
type : 'POST',
data : data,
dataType : 'json'
success:function(result)
{
//JSON processing, for now I debugg with
console.log(result);
},
error: function(qXHR, textStatus, errorThrown)
{
console.log(textStatus);
}
});
收到响应后检查并检查您的浏览器控制台。你所有的问题都解决了。
我问了一位高级程序员我在哪里工作,他建议我检查是否有任何隐藏字符被添加到响应中。在 PHP 中转换为十六进制时,没有找到隐藏字符,但在 JS 中尝试警告时出现了一个。
我将这段代码添加到我的成功函数中
function toHex(str) {
var hex = '';
for(var i=0;i<str.length;i++) {
hex += ''+str.charCodeAt(i).toString(16);
}
return hex;
}
alert(toHex(result));
而且我注意到新服务器中添加了十六进制字符“feff”。通过互联网查看,我发现这是 BOM 字符,用于通知浏览器该文件已编码。我不得不使用这个 small PHP script 并在服务器上执行它以消除错误。
显然,当您在 notepad++ 中打开文件并保存时,会发生这种情况,它可能会弄乱编码。
我最近将我的开发服务器从 PHP 5.4 升级到 PHP 5.6,我在尝试测试它时遇到了一个奇怪的错误。我的所有 PHP 代码 returns 数据通过我的主视图作为数组或字符串,它使用以下代码将数据作为 JSON 发送回任何 Ajax 查询。 (出于测试目的,我停用了 MIME 类型)
public static function SendData($data)
{
$response = array(
'status' => true,
'message' => $data
);
//header('Content-type: application/json');
exit(json_encode($response, JSON_HEX_APOS));
}
据我的 Javascript 代码使用该数据,通常立即将其解析为 JSON,因为 MIME 类型告诉 JQuery 这样做并处理它。它在 PHP 5.4 中运行良好,但自从我升级后,我的响应被 Jquery 修改了。查看服务器在 FireBug 中的响应,我发现它是有效的 JSON(使用 JSONlint 测试)
{"status":true,"message":"Didier Tartempion|dtartempion@example.com|Gestion de commerce"}
但是当我检查 firebug
中的以下 Jquery 代码时$.ajax({
url:"Connect.php",
type : 'POST',
data : data,
success:function(result)
{
//JSON processing, for now I debugg with
alert (result);
},
error: function(qXHR, textStatus, errorThrown)
{
alert(textStatus);
}
});
success 中的结果变量实际上包含我的格式奇怪的响应,使用 firebug watch 时看起来像这样:
"\n{"status":true,"message":"Didier Tartempion|dtartempion@example.com|Gestion de commerce"}"
因此,JQuery 无法解析它并且 JSONlint 说它无效。 PHP5.6 甚至可能是新服务器的某些东西可能会导致这种奇怪的行为?
谢谢
只是回应数据
echo json_encode($response, JSON_HEX_APOS);
我建议你使用 return
而不是 exit
public static function SendData($data)
{
$response = array(
'status' => true,
'message' => $data
);
//header('Content-type: application/json');
return json_encode($response, JSON_HEX_APOS);
}
在您的 ajax 调用中添加 dataType
并添加 console.log()
$.ajax({
url:"Connect.php",
type : 'POST',
data : data,
dataType : 'json'
success:function(result)
{
//JSON processing, for now I debugg with
console.log(result);
},
error: function(qXHR, textStatus, errorThrown)
{
console.log(textStatus);
}
});
收到响应后检查并检查您的浏览器控制台。你所有的问题都解决了。
我问了一位高级程序员我在哪里工作,他建议我检查是否有任何隐藏字符被添加到响应中。在 PHP 中转换为十六进制时,没有找到隐藏字符,但在 JS 中尝试警告时出现了一个。
我将这段代码添加到我的成功函数中
function toHex(str) {
var hex = '';
for(var i=0;i<str.length;i++) {
hex += ''+str.charCodeAt(i).toString(16);
}
return hex;
}
alert(toHex(result));
而且我注意到新服务器中添加了十六进制字符“feff”。通过互联网查看,我发现这是 BOM 字符,用于通知浏览器该文件已编码。我不得不使用这个 small PHP script 并在服务器上执行它以消除错误。
显然,当您在 notepad++ 中打开文件并保存时,会发生这种情况,它可能会弄乱编码。