How to upload a file via Ajax using HTTP POST (i.e. some information in the body)?

A loading popup dialog may appear

window.location = "someUrl"

or just just a link that sends an HTTP GET method, etc. I have done this successfully.

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

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

Then, on the servlet side, it reads JSON and executes a query on the database to retrieve data, and then generates Excel based on the query data.

The part that I can’t work with is the client side.

Assugming that my servlet is resources/report/schedulecreating an excel file.

This is not a popup download dialog when using Ajax :( Can someone help me how to have a boot 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()
+2
source share
1

ajax.

+10

All Articles