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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度