在get
我的响应上content-disposition: attachment;filename=f.csv
,我需要在页面上下载此文件的内容.
在$.ajax
请求我有一个错误.如何使用jQuery ajax
(或get
)获取文件内容?
UPD
error: function( jqXHR, textStatus, errorThrown ) {
console.log( jqXHR, textStatus, errorThrown );
}
Run Code Online (Sandbox Code Playgroud)
得到
Object {
...
readyState 0
responseText ""
status 0
statusText "error"
}, error,
Run Code Online (Sandbox Code Playgroud)
UPD 2
我找到了一个jquery.fileDownload插件,但它显示了带有保存或打开对话框的浏览器窗口,如下所示:
但我需要获取文件内容.
我不需要在电脑上下载文件.
UPD 3
完整代码列表:
$.ajax( {
url: link,
crossDomain: true,
dataType: "text",
success: function( data, textStatus, jqXHR ) {
alert( data );
},
error: function( jqXHR, textStatus, errorThrown ) {
console.log( jqXHR, textStatus, errorThrown );
}
} );
Run Code Online (Sandbox Code Playgroud)
文件由另一个服务生成,我无法更改它.
UPD 4
首先,我试图json
从另一个域获取数据,如下所示:
$.ajax( {
url: link,
async: true,
cache: true,
dataType: "jsonp",
crossDomain: true,
type: "GET",
jsonp: "finance_charts_json_callback",
jsonpCallback: "finance_charts_json_callback",
error: function( jqXHR, textStatus, errorThrown ) {
console.log( jqXHR, textStatus, errorThrown );
},
success: function( data, textStatus, jqXHR ) {
console.log( data );
}
} );
Run Code Online (Sandbox Code Playgroud)
link
好像 http://chartapi.finance.yahoo.com/instrument/1.0/a/chartdata;type=quote;ys=2012;yz=2;ts=1234567890/json?finance_charts_json_callback=finance_charts_json_callback
它的响应标题:
HTTP/1.1 200 OK
Date: Wed, 30 Apr 2014 12:01:08 GMT
P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO ... GOV"
Cache-Control: public
Expires: Thu, 01 May 2014 00:32:18 GMT
Last-Modified: Wed, 30 Apr 2014 00:32:18 GMT
Content-Type: text/javascript; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding,X-Ssl
Age: 0
Via: http/1.1 yts39.global.media.ir2.yahoo.com (...)
Server: ATS
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
一切正常.
当我尝试从另一台服务器获取文件时,它的响应标头:
HTTP/1.1 200 OK
Cache-Control: no-cache
Date: Wed, 30 Apr 2014 12:09:01 GMT
Pragma: no-cache
Content-Type: text/csv
Expires: -1
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
content-disposition: attachment;filename=export.csv
Content-Encoding: gzip
Vary: Accept-Encoding
Run Code Online (Sandbox Code Playgroud)
编辑、更新
尝试
$(function() {
$.getJSON("https://query.yahooapis.com/v1/public/yql?q=select"
+"* from csv where url='http://finviz.com/export.ashx?'"
+"&format=json&diagnostics=true&callback=?"
, function (data, textStatus, jqxhr) {
$.each(data.query.results.row, function (index, value) {
jqxhr.promise()
.done(function () {
$("<li>", {
"text": value.col2 + " " + value.col8
})
.css("padding", "8px")
.appendTo("ol");
})
.always(function () {
if (index === 4999) {
console.log(data.query
, data.query.diagnostics.warning
, data.query.results.row.length
, index
, $("li").length);
};
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
jsfiddle http://jsfiddle.net/guest271314/yxfEp/
归档时间: |
|
查看次数: |
8148 次 |
最近记录: |