【问题标题】:how to get the response from an ajax request with application/x-octet-stream?如何使用 application/x-octet-stream 从 ajax 请求中获取响应?
【发布时间】:2013-05-17 00:51:20
【问题描述】:

我正在使用 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 中设置不同的标头,但我的整个页面会中断

有什么想法吗?

谢谢

【问题讨论】:

  • @ShuklaSannidhya 我将如何将它与 $.ajax({.... 和 jsonp?
  • dataType: "text" 应该可以在您的 $.ajax 选项中使用
  • @Orangepill 注意。数据类型必须是 jsonp
  • 但你说它是用 CSV 响应的。
  • 当然。但是除了 jsonp 之外的任何东西都会出错。 @ShuklaSannidhya 对 xhr.responseType 提出了很好的意见,我现在正在调查,虽然我遇到了各种各样的错误

标签: php jquery ajax resources jsonp


【解决方案1】:

试试这个-

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);
}

【讨论】:

  • 302 不是错误 => 302 Found 是执行 URL 重定向的常用方法。
猜你喜欢
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 2014-06-08
  • 1970-01-01
  • 2018-11-15
  • 2020-03-30
  • 2019-06-26
相关资源
最近更新 更多