我尝试拨打谷歌网址以获取我的应用中的股票信息。
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 :
Google响应标题为:
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:附件类型响应?
这是jsfiddle
答案 0 :(得分:0)
我认为你不能。您最好的选择可能是从您自己的服务器代理请求,并删除内容处置标头。