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.
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.
Share Improve this question asked Feb 13, 2013 at 16:24 Ostap HnatyukOstap Hnatyuk 1,2762 gold badges15 silver badges21 bronze badges 1- have a look at AJAX. It's better for your purpose than submitting a form. – Sirko Commented Feb 13, 2013 at 16:26
1 Answer
Reset to default 1This 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./jQuery.post/
Or plain javascript AJAX: https://developer.mozilla/en/docs/AJAX if you want to write the whole thing yourself