jQuery get() Method

Last Updated:

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

Value Type Explanation
url Required Specifies the URL to which the 'GET' request to be sent.
data Optional Specifies data to send to the server along with the request. By default, no data is sent.
Note: data must be an object.
success Optional Specifies a function to collect the response from the server. By default the response is ignored
dataType Optional Specifies the type of data expected from the server. By default, jQuery will guess the dataType.
Possible values are
  • xml
  • json
  • script
  • text
  • html

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
get()YesYesYesYesYesYesYesYesYesYesYesYesYes

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>

Share this Page

Meet the Author