What is XMLHttpRequest?
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. POST request headers can be added using the setRequestHeader method. Below is an example of submitting JSON to the ReqBin echo URL with XMLHttpRequest object.
The new Fetch API provides an interface for fetching resources from the server. This new API provides a more powerful and flexible set of features than the XMLHttpRequest object. The Fetch API makes extensive use of "promises" that allow us to more easily handle asynchronous requests. Below is an example of sending JSON to the ReqBin echo URL with Fetch API.
If you are using jQuery in your project, it is recommended that you use jQuery Ajax methods instead of the raw XMLHttpRequest object. The jQuery $.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.
The Form submit() method posts web form to the destination server specified in the form's action attribute.
How to send an HTTP POST request in Node.js?
Node.js provides several methods for making HTTP requests. The following example sends an HTTP POST request to the ReqBin echo URL using Node.js, the "request" library.