【发布时间】:2016-02-09 10:00:56
【问题描述】:
我有一个cherrypy 服务器,我用它来从客户端生成REST API 请求。
API 中的一种方法不会像另一种那样返回 JSON,而是返回 CSV 文件。
/myMethod/report/?name=a&fromRow=1&toRow=1000
我想通过单击按钮从客户端下载此文件。但是,它必须通过cherrypy 传递,而不是直接从客户端传递。
这是我的 ajax 函数:
function myReport(name){
$.ajax( {
url : '/myMethod/myReport?name='+name,
type: 'POST',
dataType: "text",
success:function(data, textStatus, jqXHR) {
window.open(data, "Statistics Report", "width=800, height=200", true);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert('error')
}
});
}
这是我的cherrypy函数:
@cherrypy.expose
def myReport(self, name):
url = "myReport/?name=" + name + "&fromRow=1&toRow=1000"
htmlText = self.general_url(url)
cherrypy.response.headers['Content-Type'] = 'application/json'
return htmlText
值htmlText 是文件的URL。我想将此值传递给window.open。但是,传递给 window.open 的实际值是 CSV 文件的内容,而不是文件的 URL 链接(打开一个新窗口,文件内容为 URL)。我不想通过直接从ajax函数下载文件来“解决”这个问题,因为它必须通过cherrypy生成。
有人知道是什么问题吗?
【问题讨论】:
标签: javascript ajax cherrypy