我在ajax错误函数中获取了SyntaxError:JSON.parse,并且在尝试通过json验证器进行验证时,它表示标头未定义且期待{,[我的标头代码是否错误?非常感谢
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: application/json");
$json = "";
$json .= "{\n";
$json .= "\"company\": \"".$company."\",\n";
$json .= "\"box\": \"".$box."\",\n";
$json .= "\"dept\": \"".$dept."\",\n";
$json .= "\"submit\": \"".$submit."\",\n";
$json .= "\"service\": \"".$service."\",\n";
$json .= "\"address\": \"".$address."\",\n";
$json .= "\"authorised\": \"".$authorised."\"\n";
$json .= "}\n";
echo $json;
答案 0 :(得分:3)
不要试着写自己的json。如果JSON具有未转义的引号,无效字符等,则很容易变为无效。
更好地将所有内容存储在php数组中,然后使用内置的PHP
json_encode创建json。
请参阅此处的示例:http://codepad.org/Iaa0zx9J
<?php
$dataArr = array(
"company" => "abc corp",
"dept" => "finance",
"submit" => "100"
);
$myJsonString = json_encode($dataArr);
echo $myJsonString ;
?>