douren5898
2014-07-19 10:34
浏览 50
已采纳

parsererror / SyntaxError:意外的令牌<

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:

$.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 + "
Error:" + 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);

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 <

I'm using the 2.1.1 version of JQuery.

P.S.: Sorry for my english.

图片转代码服务由CSDN问答提供 功能建议

我为此搜索了很多,但我找到的答案不起作用,但我认为这个问题 是我的。

JavaScript:

  $ .ajax({
 type:'POST',
 url:url,
 data:  data,
 dataType:'json',
 success:function(response){
 if(response.status == true){
 alert('ok'); 
} else {
 alert(' 错误'); 
} 
},
错误:function(xhr,desc,err){
 console.log(xhr); 
 console.log(“Details:”+ desc +“
}:  “+ err); 
} 
}); 
   
 
 

PHP

  if(User ::)  addFavLater($ id,$ user,'favs')){
 $ result = array(“status”=&gt; true); 
} else {
 $ result = array(“status”=&gt; false)  ; 
} 
 
header('Content-type:application / json'); 
echo json_encode($ result); 
   
 
 

所以,这是一个 我所拥有的代码摘录,我想你能意识到这一点。 我希望PHP将JSON响应发送到JavaScript,但这不会发生,JavaScript会给我这个错误:

  parsererror / SyntaxError:Unexpected token&lt; 
    
 
 

我正在使用2.1.1版本的JQuery。

PS:抱歉我的英文。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhi7641 2014-07-19 10:47
    已采纳

    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).

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题