gpt4 book ai didi

javascript - Chrome 和 IE 已解析/未解析 JSON 响应

转载 作者:行者123 更新时间:2023-12-01 02:16:54 28 4
gpt4 key购买 nike

XMLhttpRequest 返回 JSON

abc.responseType = 'json';

var answer = abc.response;

如果我执行以下操作,它可以在 Chrome 中运行:

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

但是,即使 successtrue,IE 也始终会跳过 if为了让它在 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 中显然会导致以下错误:

未捕获的语法错误:JSON 中位置 1 出现意外的标记 o

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

最佳答案

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

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

关于javascript - Chrome 和 IE 已解析/未解析 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49452013/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com