我有问题。我从php返回到ajax调用时遇到一个错误。
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
在php中我做这个
echo json_encode(array("message1" => "Hi","message2" => "Something else"));
我的ajax调用
$.ajax({
type: "POST",
url: "s_save.php",
data: data,
cache: false,
dataType: "json",
beforeSend: function() {
console.log("beforesend: "+data);
},
success: function(){
console.log("form search in db: "+data);
sendMail(mailName,mailAdress, customertype );
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
console.log(ajaxOptions)
}
});
}
现在,如果我看看我的萤火虫,我会看到答案,什么对我来说很好。
{"message1":"Hi","message2":"Something else"}
错误部分,但要这样说。
200
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
parsererror
我不明白。json返回对我来说很好。为什么我会得到错误?
编辑:
在得到第一个答案后,我对php文件进行了一些更改,但没有帮助header('Content-Type:application/json');
$output = array("data" => "Hi","message2" => "Something else");
$output = str_replace(' ', '', $output);
echo json_encode($output);
编辑2
这就是解决问题的办法。感谢大家的帮助!
Save the file as UTF-8 without BOM. – Álvaro G. Vicario 12 mins ago
设置dataType:"html"而不是json。