Ajax
Ajax stands for Asynchronous Javascript And Xml. Ajax is just a means of loading data from the server to the web browser without reloading the whole page. Ajax isn't something new in web development. It is basically just creating a new XMLHttpRequest object to send and receive information to and from a web server asynchronously, in the background!
jQuery is "just" another library that tries to make its implementation easy. Let's see how it works. It provides several methods for smooth ajax calls.
Syntax:
1
$.get(URL, callback)
Copied!
A real-world example:
1
$.get("/test", function(data, status) {
2
alert("Data: " + data + "\nStatus: " + status);
3
});
Copied!
Alright. But what if you want to pass some data to the server?
Pass some data to the server:
1
$.ajax({
2
method:"GET", // can be lowercase too
3
url: "/test",
4
data: {
5
some_dumb_info: "this_is_the_info"
6
}
7
}).done(function (response){
8
console.log(response)
9
});
Copied!
Here, the data object is the payload that will be delivered to the server as request body.
Note: The method parameter is the type of Http request that you would want to perform. It can be get , post, put et cetera!
So now that you know how to send data and get response from the server, go ahead and try out making some real-world projects with this :)
Last modified 2yr ago
Copy link