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&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.
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.
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.
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.
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With