Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Convert GET URL to POST

what's the easiest way to convert a GET URL string to POST in jQuery?

e.g. I want the params of a link

    <a href="/somepage?x=1&amp;y=3" id="postlink">link</a>

to be submitted as POST onclick if javascript is activated. No AJAX, just normal form submitting.

Any Ideas?

Thanks, Hannes.

like image 272
ottsch Avatar asked Mar 17 '12 08:03

ottsch


People also ask

Can a link make a post request?

You can't. A link is a GET, by definition. It is possible to style a form submit button to look like a link, though.

What is difference between GET and POST 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.

How do you post a URL in HTML?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.


1 Answers

I just write this code, check please, may be it helpful http://jsfiddle.net/AEwxt/

$('#postlink').click(function() {
    var p = $(this).attr('href').split('?');
    var action = p[0];
    var params = p[1].split('&');
    var form = $(document.createElement('form')).attr('action', action).attr('method','post');
    $('body').append(form);
    for (var i in params) {
        var tmp= params[i].split('=');
        var key = tmp[0], value = tmp[1];
        $(document.createElement('input')).attr('type', 'hidden').attr('name', key).attr('value', value).appendTo(form);
    }
    $(form).submit();
    return false;
});
like image 63
Sergey Toy Avatar answered Sep 28 '22 03:09

Sergey Toy