【问题标题】:JSON response parsed/not parsed for Chrome and IE为 Chrome 和 IE 解析/未解析 JSON 响应
【发布时间】:2018-09-02 06:27:54
【问题描述】:

XMLhttpRequest 返回带有

的 JSON

abc.responseType = 'json';

var answer = abc.response;

如果我执行以下操作,它可以在 chrome 中工作:

if (answer.success) {
        window.alert("GOODBOY!");
      } else {
        window.alert("YOUFAILED" + answer.message);
      }

然而,IE 总是会跳过if,即使successtrue 为了让它在 Internet Explorer 中工作,我尝试解析它(再次?)

 var answer = abc.response;
 var answer2 = JSON.parse(abc.response);
      if (answer2.success) {
            window.alert("GOODBOY!");
          } else {
            window.alert("YOUFAILED" + answer2.message);
          }

这在 IE 中有效,但在 chrome 中显然会导致以下错误:

Uncaught SyntaxError: Unexpected token o in JSON at position 1

我错过了什么?我怎样才能让它在两种浏览器上都运行?

【问题讨论】:

  • 您可以使用JSON.stringify(abc, 0, 4); 打印abc 的值并检查吗?

标签: javascript json google-chrome xmlhttprequest internet-explorer-11


【解决方案1】:

由于 IE 不支持 json 作为 responseType,所以放弃它,使用默认的 text 并执行

 var answer = JSON.parse(abc.response);

【讨论】:

  • 这很容易 - 谢谢你,我会尽快标记你的答案
猜你喜欢
  • 2012-09-04
  • 2013-03-27
  • 1970-01-01
  • 2011-05-20
  • 2021-06-25
  • 1970-01-01
  • 2020-03-04
相关资源
最近更新 更多