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!

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

报告相同问题?