在获取响应时,我有内容处理:attachment; filename = f.csv,我需要在页面上下载此文件的内容.
在$.ajax请求我有一个错误.
如何使用jQuery ajax(或get)获取文件内容?
UPD
在$.ajax请求我有一个错误.
如何使用jQuery ajax(或get)获取文件内容?
UPD
error: function( jqXHR,textStatus,errorThrown ) { console.log( jqXHR,errorThrown ); }
得到
Object { ... readyState 0 responseText "" status 0 statusText "error" },error,
UPD 2
我找到了一个jquery.fileDownload插件,但它显示了浏览器窗口的保存或打开对话框,如下所示:
UPD 3
完整代码清单:
$.ajax( { url: link,crossDomain: true,dataType: "text",success: function( data,jqXHR ) { alert( data ); },error: function( jqXHR,errorThrown ) { console.log( jqXHR,errorThrown ); } } );
文件由另一个服务生成,我无法更改它.
UPD 4
首先,我试着从另一个域获取json数据,如下所示:
$.ajax( { url: link,async: true,cache: true,dataType: "jsonp",type: "GET",jsonp: "finance_charts_json_callback",jsonpCallback: "finance_charts_json_callback",errorThrown ); },jqXHR ) { console.log( data ); } } );
链接看起来像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
一切正常.
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
解决方法
编辑,更新
尝试
$(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,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); }; }); }); }); });