我需要一些帮助。我发送简单的请求到服务器,并且我期望的返回值是JSON作为数据类型。但是当我检查开发工具控制台日志时,我得到“parsererror SyntaxError {}”和“parsererror”。语法错误{} AJAX成功无法读取PHP返回的JSON
我该如何解决这个问题?以下是代码。
JQuery的
$(':submit').live('click', function() {
$.ajax({
type : 'post',
url: 'testJSON.php',
beforeSend:console.log('sending...'),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(data){
console.log(data.status);
// do magic
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
},
complete: function(XMLHttpRequest, status) {
console.log(status);
}
});
return false;
});
,这是testJSON.php
<?php
$data = array(
"status" => 1,
"firstname" => "foo",
"lastname" => "bar",
);
header('Content-type: application/json; charset=utf-8" ');
echo json_encode($data);
exit();
?>
仅供参考我使用最新版本的WAMP的。非常感谢任何帮助。
请注意,您的JSON无效,并且您已发布除JSON之外的所有内容;-) – 2013-04-29 11:03:37
@ÁlvaroG.Vicario我不确定您的无效JSON是什么意思......如果你的意思是为什么我发送请求到服务器没有JSON,只是因为需要运行PHP脚本的数据存储在$ _SESSION [] – ani 2013-04-29 14:28:12
你说你得到'parsererror'时解析JSON ...对不起,如果我误解了。 – 2013-04-29 15:05:37