2

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() 

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

3 Comments

but is it possible? I have seen many example using GET not POST tho
NO using ajax you can't trigger download file
Ok, I guess that's true.. funny though I can achieve my goal by just using standard non-ajax POST without web page being refreshed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.