Contact Form

Name

Email *

Message *

Cari Blog Ini

Ajax Post Request Vanilla Javascript

How to Send GET and POST Ajax Requests in Vanilla JavaScript

Introduction

Ajax (Asynchronous JavaScript and XML) is a technique for communicating with a web server without having to reload the entire page. This makes it possible to update parts of a web page dynamically, without affecting the rest of the page.

Sending GET Requests

To send a GET request using Vanilla JavaScript, you can use the `XMLHttpRequest` object. Here's an example: ```javascript const request = new XMLHttpRequest(); request.open('GET', 'https://example.com/api/data'); request.onload = function() { // Process the response }; request.send(); ```

Sending POST Requests

To send a POST request using Vanilla JavaScript, you can use the `XMLHttpRequest` object with the `POST` method. Here's an example: ```javascript const request = new XMLHttpRequest(); request.open('POST', 'https://example.com/api/data'); request.setRequestHeader('Content-Type', 'application/json'); request.onload = function() { // Process the response }; request.send(JSON.stringify({ name: 'John Doe' })); ```

Conclusion

Ajax is a powerful technique for creating dynamic web applications. By using Vanilla JavaScript, you can send both GET and POST requests without having to rely on a third-party library.


Comments