jQuery Ajax get and post methods

The jQuery Ajax get() and post() methods are used to request data from a server with an HTTP GET or POST request, respectively. The difference between the two methods is that GET requests retrieve data from a server, while POST requests send data to a server.
jQuery Ajax get and post methods



Here is an example of how to use the jQuery get() method to request data from a server:

$.get("ajax_info.txt", function(data) {
$("#info").html(data);
});



In this example, the get() method is used to request the file ajax_info.txt from the server. The data parameter is a callback function that will be executed when the request is complete. The callback function will receive the data that was returned by the server as its argument. In this case, the data is used to set the text content of the element with the ID info.
Here is an example of how to use the jQuery post() method to send data to a server:

$.post("ajax_test.asp", {
name: "John Doe",
age: 30
}, function(data) {
$("#results").html(data);
});


In this example, the post() method is used to send the data name and age to the server. The data parameter is an object that contains the data that will be sent to the server. In this case, the data is sent as a name-value pair. The callback function is executed when the request is complete. The callback function will receive the data that was returned by the server as its argument. In this case, the data is used to set the text content of the element with the ID results.

The jQuery get() and post() methods are a powerful tool that can be used to make requests to a server and receive data back. They can be used to implement a variety of different tasks, such as retrieving data from a database, submitting a form, or updating the content of a web page.

Post a Comment

4 Comments