The .ajax() function in jQuery is used to send asynchronous HTTP requests to a server. It is a powerful tool that can be used to perform a variety of tasks, such as retrieving data, submitting forms, and updating pages.
The .ajax() function has a number of options that can be used to customize the request. Some of the most important options include:
url: The URL of the server to send the request to.
type: The type of request to send. The default is GET, but you can also use POST, PUT, or DELETE.
data: The data to send with the request. This can be a string, an object, or an array.
success: A function to be called if the request succeeds.
error: A function to be called if the request fails.
Here is an example of how to use the .ajax() function to retrieve data from a server:
Code snippet
// Get the data from the server
$.ajax({
url: "/data",
success: function(data) {
// Do something with the data
}
});
The .ajax() function will return a jqXHR object. This object can be used to check the status of the request, get the response data, and handle errors.
Here is an example of how to check the status of the request:
Code snippet
// Check the status of the request
var xhr = $.ajax({
url: "/data",
success: function(data) {
// Do something with the data
}
});
if (xhr.status === 200) {
// The request was successful
} else {
// The request failed
}
Here is an example of how to get the response data:
Code snippet
// Get the response data
var data = $.ajax({
url: "/data",
success: function(data) {
// Do something with the data
}
}).responseText;
Here is an example of how to handle errors:
Code snippet
// Handle errors
$.ajax({
url: "/data",
error: function(xhr, textStatus, errorThrown) {
// Do something with the error
}
});
The .ajax() function is a powerful tool that can be used to perform a variety of tasks. By understanding the different options that are available, you can customize the request to meet your specific needs.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The .ajax() function in jQuery is used to send asynchronous HTTP requests to a server. It is a powerful tool that can be used to perform a variety of tasks, such as retrieving data, submitting forms, and updating pages.
The .ajax() function has a number of options that can be used to customize the request. Some of the most important options include:
Here is an example of how to use the .ajax() function to retrieve data from a server:
Code snippet
The .ajax() function will return a jqXHR object. This object can be used to check the status of the request, get the response data, and handle errors.
Here is an example of how to check the status of the request:
Code snippet
Here is an example of how to get the response data:
Code snippet
Here is an example of how to handle errors:
Code snippet
The .ajax() function is a powerful tool that can be used to perform a variety of tasks. By understanding the different options that are available, you can customize the request to meet your specific needs.