如何使用application / x-octet-stream从ajax请求获取响应?

时间:2013-05-22 05:03:51

标签: php jquery ajax resources jsonp

我正在使用jsonp从api中获取一些内容。

问题是我的链接返回了一个要保存为CSV的文件,响应为application/x-octet-stream

如果我查看Chrome开发人员工具,我会收到此错误Resource interpreted as Script but transferred with MIME type application/x-octet-stream:...Uncaught SyntaxError: Unexpected identifier

但是,如果我查看previewresponse标签,我可以看到我的数据。

有没有办法获取这些数据?

一个解决方案是让我在php中设置不同的标题,但我的整个页面将会中断

任何想法?

感谢

1 个答案:

答案 0 :(得分:0)

试试这个 -

function main() {
    var xhr;

    xhr = new XMLHttpRequest();
    xhr.open("GET", "foo.csv?r=" + Math.random(), true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            //xhr.responseText stores your CSV Content
            document.querySelector("body").innerHTML = xhr.responseText;
        }
    };
    xhr.send(null);
}