JavaScript get Method

You are Here:

JavaScript get Method

JavaScript get method loads data from the server using a HTTP GET request.

Requesting a URL

In the following example, we provide server-side coding for both PHP and Node.js, you can choose your favorite.

Example

The following is the server-side coding using PHP.

Greet.php
<?php echo "Hello User Welcome to ajax jquery"; ?>

The following is the server-side coding using Node.js.

Greet.js
var express = require("express"); var app = express(); app.get("/greet.js",function(req, res){ res.send("Hello User Welcome to ajax jquery"); }); app.listen(8000, function(){ console.log("port is listening to 8000"); });

In the following client-side coding, we are not sending any data. Instead, we request a URL using javascript GET method and receive a response.

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JavaScript get Request</h1> <button onclick="myFunction()">Click Me</button> <script> function myFunction() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { alert(this.responseText); } }; xhttp.open("GET", "/greet.php", true); xhttp.send(); } </script> </body> </html>

Syntax

open(method, url, async, user, pass);

Parameter Values

ValueTypeExplanation
methodRequiredSpecifies the request type.
Possible values are:
  • GET
  • POST
urlRequiredSpecifies the url to which the request is sent.
asyncRequiredtrue (asynchronous) or false (synchronous). Default is true.
userOptionaluser name for authentication in server side.
passOptionalpassword for authentication in server side.

Requesting a URL with Data

In the following example, we provide server-side coding for both PHP and Node.js, you can choose your favorite.

Example

The following is the server-side coding using PHP.

greet-specific.php
<?php echo "Hello ".$_GET['myname']."! Welcome to ajax jquery"; ?>

The following is the server-side coding using Node.js.

greet-specific.js
var express = require("express"); var app = express(); app.get("/greet.js",function(req, res){ res.send("Hello "+req.query.myname+"! Welcome to ajax jquery"); }); app.listen(8000, function(){ console.log("port is listening to 8000"); });

In the following client-side coding, we are requesting a URL with data using javascript GET method and receive a response.

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JavaScript get Request</h1> <button onclick="myFunction()">Click Me</button> <script> function myFunction() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { alert(this.responseText); } }; xhttp.open("GET", "/greet-specific.php?myname=Alex", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.send(); } </script> </body> </html>

Reminder

Hi Developers, we almost covered 95% of AJAX Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in AJAX.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author