简体   繁体   中英

How to get a response from a javascript post request

So I'm using this post request I found on here, but I'm wondering how I receive a response using this...

function post_to_url(path, params, method) {
    method = method || "post"; 
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }ody.appendChild(form);
    form.submit();
}

Thanks in advance.

This code configures a form and submits it. You don't get any response to javascript, because the page reloads.

Try using jQuery: http://api.jquery.com/jQuery.post/

Or plain javascript AJAX: https://developer.mozilla.org/en/docs/AJAX if you want to write the whole thing yourself

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM