The $.ajax() method in jQuery is used to send AJAX requests to the server. It has many options that allow you to customize the request and handle the response. Here is an example of how to use the $.ajax() method for complex AJAX requests in jQuery:
$.ajax({
url: "example.com/api/users",
method: "POST",
});
$.ajax({
url: "example.com/api/users",
method: "POST",
data: {
name: "John Doe",
age: 25
},
});
$.ajax({
url: "example.com/api/users",
method: "POST",
data: {
name: "John Doe",
age: 25
},
contentType: "application/json",
});
$.ajax({
url: "example.com/api/users",
method: "POST",
data: {
name: "John Doe",
age: 25
},
success: function(response) {
// Handle success response
console.log(response);
},
error: function(xhr, status, error) {
// Handle error response
console.log(error);
}
});
$.ajax({
url: "example.com/api/users",
method: "POST",
data: {
name: "John Doe",
age: 25
},
headers: {
"Authorization": "Bearer token",
"Content-Type": "application/json"
},
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {
console.log(error);
}
});
These are just a few examples of how you can use the $.ajax() method for complex AJAX requests in jQuery. You can explore more options and examples in the official jQuery documentation for the $.ajax() method.