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
<?phpecho"Hello User Welcome to ajax jquery";
?>
The following is the server-side coding using Node.js.
Greet.js
varexpress=require("express");
varapp=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>
<htmllang="en-US">
<head>
<scriptsrc="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>
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.