douren5898 2014-07-19 10:34 采纳率: 100%
浏览 55
已采纳

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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名