Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP GET request in JavaScript?

I need to do an HTTP GET request in JavaScript. What's the best way to do that?

I need to do this in a Mac OS X dashcode widget.

like image 610
mclaughj Avatar asked Oct 29 '08 16:10

mclaughj


People also ask

WHAT IS GET request in JavaScript?

The GET method of HTTP requests data from the specified source. GET requests can be cached and remain in the browser history. It can also be bookmarked. The GET method should never be used while working on sensitive data. The GET requests have length restrictions, and only should be used to get data.

What is HTTP request GET?

The HTTP GET method requests a representation of the specified resource. Requests using GET should only be used to request data (they shouldn't include data).

What is HTTP request in JavaScript?

XHR - XML Http Request XHR is a JavaScript object that is used to transfer data between a web browser and a web server. XHR is often used to request and receive data for the purpose of modifying a web page.

What is the use of GET request?

GET is used to request data from a specified resource. Some notes on GET requests: GET requests can be cached.


2 Answers

Browsers (and Dashcode) provide an XMLHttpRequest object which can be used to make HTTP requests from JavaScript:

function httpGet(theUrl) {     var xmlHttp = new XMLHttpRequest();     xmlHttp.open( "GET", theUrl, false ); // false for synchronous request     xmlHttp.send( null );     return xmlHttp.responseText; } 

However, synchronous requests are discouraged and will generate a warning along the lines of:

Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), synchronous requests on the main thread have been deprecated due to the negative effects to the user experience.

You should make an asynchronous request and handle the response inside an event handler.

function httpGetAsync(theUrl, callback) {     var xmlHttp = new XMLHttpRequest();     xmlHttp.onreadystatechange = function() {          if (xmlHttp.readyState == 4 && xmlHttp.status == 200)             callback(xmlHttp.responseText);     }     xmlHttp.open("GET", theUrl, true); // true for asynchronous      xmlHttp.send(null); } 
like image 172
Joan Avatar answered Sep 23 '22 13:09

Joan


The new window.fetch API is a cleaner replacement for XMLHttpRequest that makes use of ES6 promises. There's a nice explanation here, but it boils down to (from the article):

fetch(url).then(function(response) {   return response.json(); }).then(function(data) {   console.log(data); }).catch(function() {   console.log("Booo"); }); 

Browser support is now good in the latest releases (works in Chrome, Firefox, Edge (v14), Safari (v10.1), Opera, Safari iOS (v10.3), Android browser, and Chrome for Android), however IE will likely not get official support. GitHub has a polyfill available which is recommended to support older browsers still largely in use (esp versions of Safari pre March 2017 and mobile browsers from the same period).

I guess whether this is more convenient than jQuery or XMLHttpRequest or not depends on the nature of the project.

Here's a link to the spec https://fetch.spec.whatwg.org/

Edit:

Using ES7 async/await, this becomes simply (based on this Gist):

async function fetchAsync (url) {   let response = await fetch(url);   let data = await response.json();   return data; } 
like image 39
Peter Gibson Avatar answered Sep 22 '22 13:09

Peter Gibson