dtcuv8044
2018-12-22 18:38
浏览 394

“未捕获的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

图片转代码服务由CSDN问答提供 功能建议

我最近选择了一个我以前正在研究的项目,在做一个ajax请求时,之后 它提交,它应该返回成功消息或错误消息。 我正在使用的代码与我在我的应用程序的其余部分使用的代码相同,并且工作正常,没有任何问题。

过去7个小时左右,我遇到了这个问题,而且我自己也没有运气,因此我来这里的原因。

PHP代码:

  
 
 
  if(access){
 $ name = $ _POST ['  name']; 
 dbquery('UPDATE settings SET name =“'。escapestring($ name)。'”'); 
 $ error ['msg'] =“”; 
 echo json_encode($ error);  
 exit(); 
} else {
 $ error ['msg'] =“你没有权限。”; 
 echo json_encode($ error); 
 exit(); 
} \  n   
 
 

JS代码:

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

此代码(返回成功消息或错误消息)似乎在我的应用程序的其余部分中正常工作,但今天我无法使其工作 无论我怎么做。

编辑 - 使用console.log后(错误); ,它似乎没有定义。 问题是这个代码在我的应用程序的其他部分工作正常

log-

  
jQuery.Deferred exception:错误未定义ReferenceError  :错误未定义
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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!

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题