【发布时间】:2018-09-02 06:27:54
【问题描述】:
XMLhttpRequest 返回带有
的 JSONabc.responseType = 'json';
var answer = abc.response;
如果我执行以下操作,它可以在 chrome 中工作:
if (answer.success) {
window.alert("GOODBOY!");
} else {
window.alert("YOUFAILED" + answer.message);
}
然而,IE 总是会跳过if,即使success是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
我错过了什么?我怎样才能让它在两种浏览器上都运行?
【问题讨论】:
-
您可以使用
JSON.stringify(abc, 0, 4);打印abc的值并检查吗?
标签: javascript json google-chrome xmlhttprequest internet-explorer-11