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

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

报告相同问题?

悬赏问题

  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 AttributeError: 'EasyDict' object has no attribute 'BACKUP_DB_INFO'
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息