[英]Why do I get JSON String decode error when using extjs ajax?
Below is my code.. 下面是我的代码。
Ext.Ajax.request({
url:'test.jsp',
params:{id:id,password:password},
success:function(response){
console.log(response);
var results = Ext.util.JSON.decode(response.responseText);
if(results.success){
document.location.href="../home.jsp";
}
}
})
I got an error saying: 我收到一个错误消息:
Uncaught Error: You're trying to decode an invalid JSON String:
and console.log show 和console.log显示
Object {request: Object, requestId: 1, status: 200, statusText: "OK", responseText: "↵↵↵↵↵"…}
If I change code to (I am using url to pass my parameters): 如果将代码更改为(我正在使用url传递参数):
Ext.Ajax.request({
url:'test.jsp?id=' + id + '&password=' + password,
success:function(response){
console.log(response);
var results = Ext.util.JSON.decode(response.responseText);
if(results.success){
document.location.href="../home.jsp";
}
}
})
console.log will show console.log将显示
Object {request: Object, requestId: 1, status: 200, statusText: "OK", responseText: "{success:true,msg:'success',url:'../main.jsp'}↵↵↵↵↵"…}
and everything works. 一切正常。
Why does this happen? 为什么会这样?
you might pass true
to the second params, to make it savemode. 您可以将
true
传递给第二个参数,以使其成为savemode。
// your code should like:
Ext.util.JSON.decode(response.responseText, true);
it wouldnt solve your problem, but remove the errors. 它不会解决您的问题,但删除错误。
you still need to check your json, make sure its valid. 您仍然需要检查json,确保其有效。
An empty string isn't valid JSON. 空字符串不是有效的JSON。 You can run
JSON.parse('')
and it will throw an exception. 您可以运行
JSON.parse('')
,它将引发异常。
If you run JSON.parse('{}')
then it's ok. 如果您运行
JSON.parse('{}')
则可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.