jQuery get() Method

You are Here:

jQuery get() Method

jQuery 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 jQuery get() method and receive a response.

HTML Online Compiler
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery get() Method</h1> <p>Requesting url with no data using get() Method.</p> <button>Send Get Request</button> <script> $(document).ready(function(){ $("button").click(function(){ $.get("/greet.php",function(data){ alert(data); }); }); }); </script> </body> </html>

Syntax

$.get(url, data, success, dataType);

Parameter Values

ValueTypeExplanation
urlRequiredSpecifies the URL to which the 'GET' request to be sent.
dataOptionalSpecifies data to send to the server along with the request. By default, no data is sent.
Note: data must be an object.
successOptionalSpecifies a function to collect the response from the server. By default the response is ignored
dataTypeOptionalSpecifies the type of data expected from the server. By default, jQuery will guess the dataType.
Possible values are
  • xml
  • json
  • script
  • text
  • html

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 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 jQuery get() method and receive a response.

HTML Online Compiler
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery get() Method</h1> <p>Sending data using get() Method</p> <button>Send Get Request</button> <script> $(document).ready(function(){ $("button").click(function(){ $.get("/greet-specific.php",{ myname: "Alex" },function(data){ alert(data); }); }); }); </script> </body> </html>

Response with Specified DataType

In the following example, the response from the server is expected to be a text. But it is not necessary to mention it explicitly as by default jQuery will guess the dataType by itself.

HTML Online Compiler
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery get() Method</h1> <p>Sending data using get() Method</p> <button>Send Get Request</button> <script> $(document).ready(function(){ $("button").click(function(){ $.get("/greet-specific.php",{ myname: "Alex" },function(data){ alert(data); }, "text"); }); }); </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