I want to post parameters to a URL using the POST method but I cannot use a form. Even if I do use a form, it would have to be auto-posted with out user interaction. Is this possible? How can I do this?
In a GET request, the parameters are sent as part of the URL. In a POST request, the parameters are sent as a body of the request, after the headers. To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.
You could use a button instead of an anchor and just style the button to look like a link. That way you could have your values in hidden fields inside the same form to be sent via POST. In addition to what Ben said, you can also let the link be a dummy and have it execute a javascript that submits a hidden form.
An easy way to do a GET request without an HTML form is to use a query string: var url = "http://site.com/page?KEY=VALUE"; location.
To send an HTTP POST request, we need to first create the object by calling new XMLHttpRequest() and then use the open() and send() methods of XMLHttpRequest. To receive notifications when the status of a request has changed, we need to subscribe to the onreadystatechange event.
Using jQuery.post
$.post(
"http://theurl.com",
{ key1: "value1", key2: "value2" },
function(data) {
alert("Response: " + data);
}
);
You could use JavaScript and XMLHTTPRequest (AJAX) to perform a POST without using a form. Check this link out. Keep in mind that you will need JavaScript enabled in your browser though.
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