如何使用jQuery获取附件文件内容

get响应时,我有content-disposition: attachment;filename=f.csv ,我需要在页面上下载此文件的内容
$.ajax请求我有一个错误。 如何使用jQuery ajax (或getget文件内容?
UPD

 error: function( jqXHR, textStatus, errorThrown ) { console.log( jqXHR, textStatus, 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, textStatus, jqXHR ) { alert( data ); }, error: function( jqXHR, textStatus, errorThrown ) { console.log( jqXHR, textStatus, errorThrown ); } } ); 

文件由另一个服务生成,我无法更改它。
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 ); } } ); 

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 

一切正常。

当我尝试从另一台服务器获取文件时,它的响应标头:

 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, textStatus, jqxhr) { $.each(data.query.results.row, function (index, value) { jqxhr.promise() .done(function () { $("
  • ", { "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); }; }); }); }); });
  • jsfiddle http://jsfiddle.net/guest271314/yxfEp/

    无法获取文件内容,因为它位于不同的域中, content-disposition数据与JSONP不兼容,并且服务器不支持通过Access-Control-Allow-Origin标头进行CORS(跨源资源共享) 。

    您的第一个请求有效,因为服务器正在使用JSONP进行响应。 但是,您的content-disposition请求正在接收原始数据(没有CORS就无法访问)。

    JSONP通过将资源请求为 ,服务器使用JavaScript函数调用进行响应,并将数据作为参数传递。 因此,当您收到脚本时,浏览器会执行该脚本,您可以访问该函数中的数据。

    content-disposition请求通过让服务器输出文件的原始内容来工作,并且没有像JSONP中那样的JavaScript函数调用,因此尽管浏览器接收数据,但它不允许您访问它。

    唯一两种可能的解决方案是:

    • 请服务器管理员启用CORS。
    • 通过与JavaScript相同的域上的服务器端脚本代理您的ajax请求,这不会受到相同的跨域限制,因为它将是服务器到服务器的请求。

    只是一个想法。 但是你试过在服务器端生成不同的内容吗? 比如将这些csv反序列化为对象(使用您使用的任何语言),然后只将setial化的对象返回到json内容中。