简体   繁体   English

为什么在使用extjs ajax时出现JSON字符串解码错误?

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

相关问题 如何获取JSON字符串并使用AJAX显示 - How DO I get JSON String And display Using AJAX 为什么在解码 URI 编码的 JSON 字符串时会收到错误“解析 JSON 字符串时出现意外的字符串结尾”? - Why do I get the error “unexpected end of string while parsing JSON string” when decoding a URI-encoded JSON string? 尝试在 ExtJS4.2 中的网格上使用缓冲渲染器时,为什么会出现“无方法 'indexOf'”错误 - Why do I get "no method 'indexOf'" error when trying to use a Buffered Renderer on a grid in ExtJS4.2 为什么尝试使用Stringify将数组转换为JSON字符串时出现方括号 - Why do I get square brackets when trying to convert array to JSON string using stringify 为什么在尝试解析这个 json 对象时会出错? - Why do I get error when trying to parse this json object? 如何在ExtJS中为AJAX调用创建JSON? - How do I create JSON in ExtJS for an AJAX call? 当我使用 jquery ajax 运行 $.post 时,当我使用 json 参数解码时,我的帖子为空。 我该如何解决? - When I run a $.post with jquery ajax, my Post is empty when I decode with the json parameter. How do I fix that? 使用“dotnet new react -o my-app”中的默认代码时,为什么会出现 JSON.parse 错误? - Why do I get JSON.parse error when using default code from “dotnet new react -o my-app”? 如何使用ajax json获取正确的错误消息 - how do get correct error message using ajax json 为什么要发布JSON文件HTTP REQUEST时出现错误400 - Why do I get a Error 400 when I want to post a JSON file HTTP REQUEST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM