0

我尝试调用谷歌 URL 来获取我的应用程序中的股票信息。

JavaScript 代码如下:

var url = "http://www.google.com/finance/match?matchtype=matchall&q=msft";
$.ajax({
    type: "GET",
    dataType: "jsonp",
    contentType: 'application/json; charset=utf-8',
    url: url
}).done(function (data) {
    console.log(data);
});

从控制台日志中,运行结果为:

Uncaught SyntaxError: Unexpected token :

谷歌响应头是:

Alternate-Protocol:80:quic
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Disposition:attachment; filename="json.txt"; filename*=UTF-8''json.txt
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
Date:Thu, 24 Oct 2013 04:52:40 GMT
Expires:Fri, 01 Jan 1990 00:00:00 GMT
Pragma:no-cache
Server:GSE
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block

响应正文是格式良好的 JSON

{"matches":[{"t":"MSFT","n":"Microsoft Corporation","e":"NASDAQ","id":"358464"},{"t":"MSFT34","n":"MICROSOFT -DRN","e":"BVMF","id":"229763171687562"}]}

但响应是附件,因为响应标头指定Content-Disposition:attachment; filename="json.txt"; filename*=UTF-8''json.txt

如何处理 JavaScript 中的 Content-Disposition:attachment 类型响应?

这是jsfiddle

4

1 回答 1

0

我不认为你可以。您最好的选择可能是代理来自您自己的服务器的请求,并去掉 content-disposition 标头。

于 2013-10-24T05:29:38.167 回答