针对Chrome和IE解析/未解析JSON响应

时间:2018-03-23 14:30:48

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

XMLhttpRequest返回带有

的JSON

abc.responseType = 'json';

var answer = abc.response;

如果我执行以下操作,它将在chrome中运行:

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

然而,即使ifsuccess,IE仍会跳过true 为了在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

我错过了什么?如何让它在两种浏览器上都能正常工作?

1 个答案:

答案 0 :(得分:2)

由于IE不支持json作为responseType,请删除它,使用默认text并执行

 var answer = JSON.parse(abc.response);
相关问题