parsererror / SyntaxError:意外的令牌<

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.

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

JavaScript: p>

  $ .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); 
} 
}); 
  code>  pre> 
 
 

PHP p>

  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); 
  code>  pre> 
 
 

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

  parsererror / SyntaxError:Unexpected token&lt; 
  code>   pre> 
 
 

我正在使用2.1.1版本的JQuery。 p>

PS:抱歉我的英文。 p> div >

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