最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) - Stack Ov

programmeradmin2浏览0评论

i know that there's so many questions about this but sorry im really confuse about this error...

i created a login page and i used ajax for the POST Request..what happens is when i used this

$.ajax({
 url:'../ajax/checklogin.php',
 type:'POST',
 dataType:'JSON',
 data:$('form').serialize(),   
  success: function(result){
  $.post("../www/login.php",{ users_id: JSON.parse(result.users_id)}).done(window.location.href='../www/index.php');
  }
});

the $.post is working but when i tried to add another field to parse i got this error.. the error is on users_active.. for some reason i dont have any idea why i got the error

$.post("../www/login.php",{ users_id: JSON.parse(result.users_id),users_active: JSON.parse(result.users_active)}).done(window.location.href='../www/index.php');

the other field are fine but the only field that gives me error is the users_active.. i even check the json array that is being returned they are valid json...

i know that there's so many questions about this but sorry im really confuse about this error...

i created a login page and i used ajax for the POST Request..what happens is when i used this

$.ajax({
 url:'../ajax/checklogin.php',
 type:'POST',
 dataType:'JSON',
 data:$('form').serialize(),   
  success: function(result){
  $.post("../www/login.php",{ users_id: JSON.parse(result.users_id)}).done(window.location.href='../www/index.php');
  }
});

the $.post is working but when i tried to add another field to parse i got this error.. the error is on users_active.. for some reason i dont have any idea why i got the error

$.post("../www/login.php",{ users_id: JSON.parse(result.users_id),users_active: JSON.parse(result.users_active)}).done(window.location.href='../www/index.php');

the other field are fine but the only field that gives me error is the users_active.. i even check the json array that is being returned they are valid json...

Share Improve this question asked May 11, 2017 at 8:29 Lion SmithLion Smith 6553 gold badges19 silver badges50 bronze badges 5
  • Why the JSON.parse()? result should already be an object – Andreas Commented May 11, 2017 at 8:31
  • the reason i use parse is to remove the " " to make it a string.. – Lion Smith Commented May 11, 2017 at 8:32
  • if i console console.log(JSON.parse(result.users_active)); i got the error title but for the users_id when i console console.log(JSON.parse(result.users_id)); instead of getting "12345" the result is 12345 thats the reason why i use parse to remove the " " to make it a plain string. – Lion Smith Commented May 11, 2017 at 8:39
  • Can you show us the content of result? (console.log(result);) – antesoles Commented May 11, 2017 at 8:39
  • {"id":"26","users_id":"201710001","users_username":"123","users_active":"active"} this is the result of console.log i just removed some fields – Lion Smith Commented May 11, 2017 at 8:47
Add a ment  | 

2 Answers 2

Reset to default 2

JSON.parse() takes a string and turns it into a JavScript object.

You are lucky, that JSON.parse("12345") can be converted to new Number("12345"), which is indeed an integer.

result.users_active is already a JavaScript object (or array) or maybe a String, which not represents a JSON object, so the parse will result in a syntax error as stated in https://www.w3schools./js/js_json_parse.asp.

I assume, you need JSON.stringify(), but to assure that, you should post some code or your result object. https://www.w3schools./js/js_json_stringify.asp

Edit: Now that I have seen the object, "active" is a simple string and you can't strip the quotes. So just use users_active: result.users_active.

JSON.parse doesn't remove quotes ("), but converts your data into objects, booleans or strings - if they were valid json before (not just the values of the properties, which you have passed). See here: https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

If you want to assure that you pass integers or strings (or whatever you need) to your ajax post, just use the adequate javascript functions, like parseInt() or String().

See here an example with your data:

var result = {
  "id":"26",
  "users_id":
  "201710001",
  "users_username":"123",
  "users_active":"active"
  };

$.post("../www/login.php", { 
    users_id: parseInt(result.users_id),
  users_active: String(result.users_active) 
  }).done(window.location.href='../www/index.php');
发布评论

评论列表(0)

  1. 暂无评论