javascript-request tagged requests and articles

Categorized request examples and articles tagged with [javascript-request] keyword
How do I send a POST request using JavaScript?
To make an HTTP POST request from a web browser, JavaScript offers two primary methods: the promise-driven fetch() API and the callback-driven XMLHttpRequest object. To send POST requests from Node.js, you can use the native "http" module. To make a POST request with JavaScript Fetch API, you must indicate the HTTP POST method name with the "method" parameter and the POST payload in the "body" parameter. Extra HTTP request headers can be set with the "headers" parameter. In this JavaScript POST request example, we send a POST request to the ReqBin echo URL using the fetch() method. Below are additional examples of sending JavaScript POST requests with XMLHttpRequest object. Click "Execute" to make a JavaScript POST request online and see the result.

How to send Bearer Token with JavaScript Fetch API?
To send a Bearer Token in an Authorization header to a server using the JavaScript Fetch API, you must pass the "Authorization: bearer {token}" HTTP header to the fetch() method using the "headers" parameter. Bearer Token is an encrypted string returned by the server and stored on the user's computer that authenticates the user to access protected resources on the server. You must pass a Bearer Token to the fetch() method for every request you make to a protected resource. In this JavaScript Fetch API Bearer Token example, we send a request with an Authorization header to the ReqBin echo URL using the fetch() method. Click Execute to run the JavaScript Fetch API Bearer Token Authorization Header example online and see the result.

How to make a GET request using JavaScript?
To make a GET request with JavaScript, call the fetch() method and provide the target URL. The fetch() uses GET by default if no HTTP method is passed. To send additional HTTP headers to the server with your JavaScript GET request, pass them with "headers" parameters. If you want to include credentials in your GET request, set credentials: "include". For older browsers, you can use the XMLHttpRequest object to make HTTP requests. In this JavaScript GET Request example, we send a request to the ReqBin echo URL using the fetch() method with a custom HTTP header. Below are additional examples of JavaScript GET requests with detailed descriptions. Click "Execute" to run the JavaScript GET Request online and see the result.

How to make requests using XMLHttpRequest in JavaScript?
To make a request using a JavaScript XMLHttpRequest object, you must first create an XMLHttpRequest object and then open the target URL by calling the xhr.open() method. POST data can be sent to the server by passing it to the xhr.send() method. Custom HTTP headers can be added to the request using the xhr.setRequestHeader(). The xhr.onload event will be fired after the XMLHttpRequest object has completed the asynchronous request. An xhr.status value of 200x indicates that the request was successful, 300x indicates a redirect, and 400x and 500x indicate an error. In this JavaScript XMLHttpRequest Request example, we make a request to the ReqBin echo URL. Click Execute to run JavaScript XMLHttpRequest example online and see the result.

How to send Authorization Credentials with JavaScript Fetch API?
To send authorization credentials using the Fetch API in JavaScript, you need to allow the credentials to be sent to the server by adding the «credential: 'include'» parameter when calling the fetch() method. Default Fetch API requests do not contain user credentials such as cookies and HTTP authentication headers. This is done for security reasons because user authentication data allows JavaScript to act on behalf of the user and obtain private information. If you want to send credentials only to the original domain, use the «credentials: 'same-origin'» parameter. To prevent the browser from sending credentials at all, use the «credentials: 'omit'» option. In this JavaScript Fetch API with Credentials example, we send a request with «credential: 'include'» parameter to the ReqBin echo URL using the fetch() method. Click Execute to run the JavaScript Fetch API with Credentials example online and see the result.

How do I send an AJAX request in JavaScript?
There are two ways to make AJAX calls in the browser with JavaScript: the XMLHttpRequest object and Fetch API, and several client libraries such as jQuery, Axios, and SuperAgent that use one of these two methods. The XMLHttpRequest() method is the oldest and most popular way to send AJAX requests to a server in JavaScript. The Fetch API is a new, more flexible, and easier way to make asynchronous HTTP requests to a server and is based on JavaScript Promises. To send AJAX requests to Node.js, you can use the built-in http module. In this JavaScript AJAX Request Example, we use the XMLHttpRequest() method to send asynchronous HTTP requests to the ReqBin echo URL. Click Execute to run JavaScript AJAX Request Example online and see the result.