Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery $.post() - Possible to do a full page post request?

Tags:

jquery

post

I know I can do an out of band Post request with jQuery and the $.post() syntax. However, I'm interested to know if it is possible for jQuery to cause a post request on the whole page (as when a form is submitted) so that a brand new page is loaded. Is this possible?

There is no form element in the DOM, so I can't do form.submit().

like image 812
UpTheCreek Avatar asked Nov 10 '09 14:11

UpTheCreek


People also ask

How does jQuery POST work?

The jQuery post() method sends asynchronous http POST request to the server to submit the data to the server and get the response. Syntax: $. post(url,[data],[callback],[type]);

What is get and post method in jQuery?

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

What is difference between Ajax and POST?

$. post is a shorthand way of using $. ajax for POST requests, so there isn't a great deal of difference between using the two - they are both made possible using the same underlying code.

What is post method in Ajax?

Sends an asynchronous http POST request to load data from the server. Its general form is: jQuery. post( url [, data ] [, success ] [, dataType ] ) url : is the only mandatory parameter.


1 Answers

I use this function when I need to send data and don't have form in the DOM:

function postData(actionUrl, method, data) {
    var mapForm = $('<form id="mapform" action="' + actionUrl + '" method="' + method.toLowerCase() + '"></form>');
    for (var key in data) {
        if (data.hasOwnProperty(key)) {
            mapForm.append('<input type="hidden" name="' + key + '" id="' + key + '" value="' + data[key] + '" />');
        }
    }
    $('body').append(mapForm);
    mapForm.submit();
}

And I call it like this:

var data= { 'property1': 666, 'property2': 'I am a boy' };
postData('http://urltopostdata.to', 'post', data);
like image 66
Kent-Remi Gabrielsen Avatar answered Oct 19 '22 23:10

Kent-Remi Gabrielsen