How do you make a simple POST request in Javascript without using a forms and without posting back?
post() method allows you to post data to the server in a single line. This is a simple wrapper for the more advanced $. ajax method. Below is an example of sending JSON to the ReqBin echo URL with jQuery Ajax method. JavaScript POST request with jQuery Ajax.
POST data is data that is handled server side. So there is no way you can read a post data using JavaScript.
By design, the POST request method requests that a web server accept the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form. In contrast, the HTTP GET request method retrieves information from the server.
To make a POST request to an API endpoint, you need to send an HTTP POST request to the server and specify a Content-Type request header that specifies the data media type in the body of the POST request. The Content-Length header indicates the size of the data in the body of the POST request.
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);
You can do this using AJAX calls (XMLHttpRequest object)
http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
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