How do you make a simple POST request in Javascript without using a forms and without posting back?
-
docs.jquery.com/Ajax/jQuery.post#urldatacallbacktypeJoshua Kifer– Joshua Kifer2009-03-28 05:02:45 +00:00Commented Mar 28, 2009 at 5:02
-
this is too vague, and you could get started via google queryJosh Stodola– Josh Stodola2009-03-28 06:00:31 +00:00Commented Mar 28, 2009 at 6:00
-
1possible duplicate of JavaScript post request like a form submitmikemaccana– mikemaccana2014-05-07 14:50:44 +00:00Commented May 7, 2014 at 14:50
-
In my answer you can do a request invisible to the user without AJAX, popups or refresh the page. Of course, you don't retrieve a response but it's usefull for RESTFul API without AJAX.SnakeDrak– SnakeDrak2014-08-21 10:41:15 +00:00Commented Aug 21, 2014 at 10:41
Add a comment
|
3 Answers
Though I am taking the code sample from @sundeep answer, but posting the code here for completeness
var url = "sample-url.php";
var params = "lorem=ipsum&name=alpha";
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
//Send the proper header information along with the request
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(params);
1 Comment
Austin Burk
So simple, I love it :D
You can do this using AJAX calls (XMLHttpRequest object)
1 Comment
SnakeDrak
I think if he wanted to use AJAX he would have used. In my answer I have made a post request without
AJAX and without open a page, popup or refresh the page :).I have made a function that sends a request without refreshing the page, without opening a page, and without AJAX. The process is invisible to the user. I use a false iframe to send a request:
/**
* Make a request without Ajax and without refreshing the page
* Invisible for the user
* @param url string
* @param params object
* @param method string get or post
**/
function requestWithoutAjax( url, params, method ){
params = params || {};
method = method || "post";
// function to remove the iframe
var removeIframe = function( iframe ){
iframe.parentElement.removeChild(iframe);
};
//Make an iframe...
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.onload = function(){
var iframeDoc = this.contentWindow.document;
// Make a invisible form
var form = iframeDoc.createElement('form');
form.method = method;
form.action = url;
iframeDoc.body.appendChild(form);
// pass the parameters
for( var name in params ){
var input = iframeDoc.createElement('input');
input.type = 'hidden';
input.name = name;
input.value = params[name];
form.appendChild(input);
}
form.submit();
//Remove the iframe
setTimeout( function(){
removeIframe(iframe);
}, 500);
};
document.body.appendChild(iframe);
}
Now you can do it:
requestWithoutAjax('url/to', { id: 2, price: 2.5, lastname: 'Gamez'});
See how works!: http://jsfiddle.net/b87pzbye/10/.
3 Comments
Domino
EDIT: seems somebody figured out how to make a working version which doesn't raise a potential XSS attack flag. jsfiddle.net/b87pzbye/37
Geremia
"without open[ing] a page" You're opening a page in an iframe.
SnakeDrak
@Geremia I appreciate the clarification and understand your perspective. The iframe does indeed load a page, but it's done in a way that's invisible and non-disruptive to the user, aligning with the function's intent.