Safari将来自xhr请求的对象解释为字符串而不是对象

Safari interprets Objects from xhr request as String not as Objects

本文关键字:对象 字符串 解释 Safari xhr 请求 将来      更新时间:2023-09-26

我正在尝试通过XHR请求加载一个json文件。

var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.responseType = "jsonp";
    xhr.onload = function (data) {
        discog = xhr.response;
    };
    xhr.send();

这在chrome中工作得很好,但是safari将响应解释为字符串-我在这里做错了什么?

非常感谢

一个问题是"jsonp"不是有效的responseType

试试这个:

var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function(){
    discog = JSON.parse(xhr.responseText);
};
xhr.send();

或:

var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "json";
xhr.onload = function(){
    discog = xhr.response;
};
xhr.send();