简体   繁体   English

未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> )

[英]Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>)

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作为POST请求。当我使用它时会发生什么

$.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正在工作,但是当我试图添加另一个字段来解析我得到了这个错误..错误是在users_active ..由于某种原因我不知道为什么我得到错误

$.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... 另一个字段很好但是唯一给我错误的字段是users_active ..我甚至检查正在返回的json数组它们是有效的json ...

JSON.parse() takes a string and turns it into a JavScript object. JSON.parse()接受一个字符串并将其转换为JavScript对象。

You are lucky, that JSON.parse("12345") can be converted to new Number("12345") , which is indeed an integer. 你很幸运, JSON.parse("12345")可以转换为new Number("12345") ,这确实是一个整数。

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.com/js/js_json_parse.asp . result.users_active已经是一个JavaScript对象(或数组),或者可能是一个不代表JSON对象的String,因此解析将导致语法错误,如https://www.w3schools.com/js/js_json_parse中所述。 asp

I assume, you need JSON.stringify() , but to assure that, you should post some code or your result object. 我假设您需要JSON.stringify() ,但为了确保这一点,您应该发布一些代码或结果对象。 https://www.w3schools.com/js/js_json_stringify.asp https://www.w3schools.com/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. 编辑:现在我已经看到了对象, "active"是一个简单的字符串,你不能去除引号。 So just use users_active: result.users_active . 所以只需使用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.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse JSON.parse不会删除引号(“),但会将您的数据转换为对象,布尔值或字符串 - 如果它们之前是有效的json(不仅仅是您已经传递的属性的值)。请参阅: https:/ /developer.mozilla.org/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(). 如果你想确保将整数或字符串(或任何你需要的东西)传递给你的ajax帖子,只需使用适当的javascript函数,如parseInt()或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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 : at JSON.parse (<anonymous> ) 在对象。<anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous> Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous> )</anonymous> - SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 at JSON.parse (<anonymous> ) 在 XMLHttpRequest.xmlhttp.onreadystatechange</anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.xmlhttp.onreadystatechange 未捕获到的SyntaxError:JSON中的意外令牌s在JSON.parse( <anonymous> ) - Uncaught SyntaxError: Unexpected token s in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> ) at./src/context/AuthContext.js</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at ./src/context/AuthContext.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM