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!

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

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题