parsererror / SyntaxError:意外的令牌< [英] parsererror / SyntaxError: Unexpected token <
问题描述
我为此搜索了很多,但我找到的答案不起作用,但我认为问题是我的。
I've searched a lot for this but the answers that I find doesn't work but I think that the problem is mine.
JavaScript:
JavaScript:
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
success: function(response) {
if(response.status == true) {
alert('ok');
} else {
alert('error');
}
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
PHP
if(User::addFavLater($id, $user, 'favs')) {
$result = array("status" => true);
} else {
$result = array("status" => false);
}
header('Content-type: application/json');
echo json_encode($result);
所以,这是我所拥有的代码的摘录,我想你能意识到这一点。我希望PHP将JSON响应发送到JavaScript但这不会发生,JavaScript会给我这个错误:
So, this is an excerpt of code that I have and I think can you realising. I want PHP to send a JSON response into JavaScript but this is not happening and JavaScript gives me this error:
parsererror / SyntaxError: Unexpected token <
我正在使用2.1.1版本的JQuery。
I'm using the 2.1.1 version of JQuery.
PS:对不起我的英语。
P.S.: Sorry for my english.
推荐答案
方法 $。ajax
,成功回调在变量 data
中接收服务器(JSON文件)的响应( success:function(数据){...}
)。因此,尝试从此变量中恢复所需的值(在您的情况下,类似 data.response
而不仅仅是响应
)。
In the method $.ajax
, the success callback receives the response of the server (the JSON file) in the variable data
(success:function(data){...}
). Thus, try to recover the value you want from this variable (in your case, something like data.response
rather than just response
).
这篇关于parsererror / SyntaxError:意外的令牌<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!