dtcuv8044 2018-12-22 18:38
浏览 451
已采纳

“未捕获的SyntaxError:JSON中出现意外的令牌H”

I've recently picked up a project I was working on a bit ago, and when doing an ajax request, after it submits, it should return a success message or an error message. The code I'm using is the same code I'm using on the rest of my application, and it is working fine without any issues.

I've had the issue for the past 7 or so hours, and I have had no luck fixing it myself, hence why I'm coming here.

PHP Code:



if (access) {
  $name = $_POST['name'];
  dbquery('UPDATE settings SET name="' . escapestring($name) . '"');
  $error['msg'] = "";
  echo json_encode($error);
  exit();
} else {
  $error['msg'] = "You don't have permission.";
  echo json_encode($error);
  exit();
}

JS Code:



$(document).ready(function () {
    $('#updateName').ajaxForm(function (error) {
        error = JSON.parse(error);
        if (error['msg'] === "") {
            toastr.success('Name Updated', 'System:', {timeOut: 10000})
        } else {
            toastr.error(error['msg'], 'System:', {timeOut: 10000})
        }
    });
});

This code (the returning either success message or error message) seemingly works fine throughout the rest of my application, yet today I haven't been able to get it working no matter what I try.

Edit- After using console.log(error); , it appears it's not defined. The issue is that this code works fine on other parts of my application

log-


jQuery.Deferred exception: error is not defined ReferenceError: error is not defined
  • 写回答

1条回答 默认 最新

  • dousha1831 2018-12-23 14:10
    关注

    First of all, error = JSON.parse(error); The error your trying to access is not defined. it shoudl be var error = JSON.parse(error); that is why youre getting an error or undefined variable error.

    But the JSON error

    “Uncaught SyntaxError: Unexpected token H in JSON”

    is different. Can you please make a fiddle and post ur code!

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

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?