[英]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.