How to get response from javascript post request

So, I am using this post request I found here, but I am wondering how I get the 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.

+3


source to share


1 answer


This code sets up the form and submits it. You are getting no javascript response because the page is reloading.

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



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

+1


source







All Articles