O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending a GET Request from JavaScript

var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() {   if (xmlhttp.readyState==4 && xmlhttp.status==200)     { document.getElementById("email").innerHTML=xmlhttp.responseText;}} xmlhttp.open("GET","getUserEmail.php?userid=brad",true); xmlhttp.send();

To send a GET request from JavaScript, you first need to implement an onreadystatechange event handler to handle the response or responseText values. Then you need to call open("GET", URL) to open the connection and send() to send the request.

The following code shows an example of a GET request that is sent to the web server via JavaScript. The user value is added to the query string in line 15. Figure ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required