如何通过Ajax下载文件HTTP POST(即身体有些信息)?身体、文件、信息、Ajax

2023-09-10 14:28:20 作者:情深不寿

下载弹出的对话框中可以通过

显示

 了window.location =someUrl
 

或者只是具有发送HTTP GET方法等的链接。我已经成功地做​​到了这一点。

但现在我想要做的Ajax和HTTP POST。在POST身体有JSON像

  {VAL1:KEY1,val2的:键2}
 

然后在servlet的一面,它读取JSON和执行查询对数据库获取数据,然后生成Excel的基于查询的数据。

我无法得到它的工作的部分是客户端。

Assugming我的servlet在资源/报告/进度生成Excel文件。

使用Ajax时不弹出下载对话框:( 任何人可以帮助我如何让下载对话框与阿贾克斯?

 函数post25(){
            变种jsonInput = {};
            jsonInput ['作业区コード'] =481;
            jsonInput ['机械コード'] =11;
            jsonInput ['作业日'] =2000年1月1日;
            jsonInput = JSON.stringify(jsonInput);

            VAR ajaxRequest =新XMLHtt prequest();
            ajaxRequest.onreadystatechange =功能(){
                如果(ajaxRequest.readyState == 4和&安培; ajaxRequest.status == 200){
                    VAR解析度= ajaxRequest.responseText;
                    //location.href =../resources/report/schedule;
                }
                否则,如果(ajaxRequest.status == 409 || ajaxRequest.status == 500 || ajaxRequest.status == 204){
                    警报(ajaxRequest.status);
                    的document.getElementById(showMessage)的innerHTML = ajaxRequest.responseText。
                }
            }
            ajaxRequest.open(POST,../resources/report/schedule,真正的);
            ajaxRequest.setRequestHeader(内容类型,应用/ JSON);
            ajaxRequest.send(jsonInput);
        } //结束post25()
 
Ajax

解决方案

出于安全原因的不允许使用AJAX 以下载文件。

Download popup dialog can be displayed by

window.location = "someUrl"

or just simply have a link that send HTTP GET method and so on. I've done this successfully.

But now I want to do Ajax with HTTP POST. The POST body has JSON like

{"val1":"key1", "val2":"key2"}

Then in servlet side, it read the JSON and execute query against DB to get data then generate Excel based on the query data.

The part I can't get it working is client side.

Assugming that my servlet at resources/report/schedule generates Excel file.

This does not popup download dialog when using Ajax :( Can anybody help me how to have download dialog with Ajax?

  function post25() {
            var jsonInput = {};
            jsonInput['作業区コード'] = "481";
            jsonInput['機械コード'] = "11";
            jsonInput['作業日'] = "2000/01/01";
            jsonInput = JSON.stringify(jsonInput);

            var ajaxRequest = new XMLHttpRequest();
            ajaxRequest.onreadystatechange = function() {
                if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
                    var res = ajaxRequest.responseText;
                    //location.href = "../resources/report/schedule";
                }
                else if(ajaxRequest.status == 409 || ajaxRequest.status == 500 || ajaxRequest.status == 204) {
                    alert(ajaxRequest.status);
                    document.getElementById("showMessage").innerHTML = ajaxRequest.responseText;
                }
            }
            ajaxRequest.open("POST", "../resources/report/schedule", true);
            ajaxRequest.setRequestHeader("Content-Type", "application/json");
            ajaxRequest.send(jsonInput); 
        }//end post25()

解决方案

For security reason it is not allowed to download file using ajax.