The task is to show how the XMLHttpRequest can be used to POST data to an API by making custom HTTP library. We will be taking a fake API which will contain Array of objects as an example and from that API we will show to POST data by XMLHttpRequest method by making a custom HTTP library.
What is Ajax?
Note: First make a HTML file and add the HTML markup according to the requirement. In the bottom of the body tag attach two scripts file as library.js and app.js in the same order.
Steps required to make library.js File:
- In library.js file, make a function easyHTTP to initialize a new XMLHttpRequest() method.
- Set easyHTTP.prototype.post to a function which contains three parameters ‘url’, data and callback.
- Now open an object using this.http.open function. It takes three parameters, the first one is request type (GET or POST or PUT or DELETE), second is the URL for the API and last one is a boolean value (true means asynchronous call and false means synchronous call).
- Now we will use onload function to display the data. But before that first we need to set content-type with this.http.setRequestHeader method and also assign this keyword to self to have scope of this keyword into onload function. The onload function is executed after the API call is done. This function will run a callback function which has two arguments as error and response text.
- Last step is to send the request using the send() function. It should be noted that send() function needs to send data after converting object data to string using JSON.stringify(data).
Steps required to make app.js File:
- First of all instantiate easyHTTP with new keyword.
- Create a custom data(object) to post.
- Pass URL, data and a callback function in post prototype function.
- The callback function contains two arguments error to print if any error occurs and response to get the actual response.
Open your index.html file in any browser and open its console by right click->Inspect element->console. Hence you will see the below result.
Note: It contains one additional key-value pair as ‘id’ 101. Don’t worry about this as this is created by default.
- Get request using AJAX by making Custom HTTP library
- Simple GET and POST request using Fetch API method by making custom HTTP library
- DELETE request using XMLHttpRequest by making Custom HTTP library
- How to make PUT request using XMLHttpRequest by making Custom HTTP library ?
- How to make simple PUT request using fetch API by making custom HTTP library ?
- Simple DELETE request using fetch API by making custom HTTP library
- HTTP Request vs HapiJS Request in Node.js
- How HTTP POST request work in node.js ?
- Simple POST request using the fetch API
- HTTP headers | Access-Control-Request-Headers
- HTTP headers | Access-Control-Request-Method
- How to select and upload multiple files with HTML and PHP, using HTTP POST?
- How to display search result of another page on same page using ajax in JSP?
- How to set timeout for ajax by using jQuery?
- How to use simple API using AJAX ?
- HTTP GET and POST Methods in PHP
- Difference between Fetch and Axios.js for making http requests
- Ajax | Introduction
- How ajax works? Difference between angular js and jquery
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.