在Ember的AJAX Post之后没有文件下载

时间:2022-12-28 09:48:58

I have an AJAX Post request located in one of my ember controllers to a Node.js server :

我在我的一个ember控制器中有一个AJAX Post请求到Node.js服务器:

$.ajax({
          type: "POST",
          url: "https://localhost/api/report",
          contentType: "application/json",
          data: payload,
          headers: {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                    "Cache-Control": "no-cache",
                    "Pragma": "no-cache"
                    }
        });

The Post request is successful and returns with the response headers:

Post请求成功并返回响应标头:

Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Disposition:attachment; filename="report.pdf"
Content-Type:application/pdf
Date:Wed, 15 Jul 2015 19:11:23 GMT
File-Extension:pdf
Number-Of-Pages:1
Transfer-Encoding:chunked
Vary:Origin
X-Powered-By:Express
X-XSS-Protection:0

This response should initiate a file download within the browser, but nothing happens. I have tested the same request to the same location from outside of my Ember application and it successfully initiates the download.

此响应应在浏览器中启动文件下载,但没有任何反应。我已经从我的Ember应用程序外部对同一位置测试了相同的请求,并成功启动了下载。

What is the issue with my request/response? Am I going about this the wrong way for Ember?

我的请求/回复有什么问题?我对Ember的看法是错误的吗?

1 个解决方案

#1


4  

It's not an issue with Ember, it's an issue with javascript. You can't make an ajax request that starts a file download like that. You can however create an iframe, a form, post to the iframe and that will in turn trigger the file download.

这不是Ember的问题,这是javascript的一个问题。您不能发出启动文件下载的ajax请求。但是,您可以创建iframe,表单,发布到iframe,然后依次触发文件下载。

See:

#1


4  

It's not an issue with Ember, it's an issue with javascript. You can't make an ajax request that starts a file download like that. You can however create an iframe, a form, post to the iframe and that will in turn trigger the file download.

这不是Ember的问题,这是javascript的一个问题。您不能发出启动文件下载的ajax请求。但是,您可以创建iframe,表单,发布到iframe,然后依次触发文件下载。

See: