Table of Contents
1. url: The URL address to which the request is to be sent.
2. type: type of request, optional parameters, including GET, POST, PUT, DELETE, etc. The default is GET.
3. data: The data to be sent to the server, which can be a string, array or object. Default is null.
4. dataType: The type of response data returned from the server. Optional parameters include “xml”, “json”, “html”, “text”, etc. The default is intelligent guessing.
5. contentType: The data format for sending requests, optional parameters, the default is “application/x-www-form-urlencoded”. Another commonly used one is “application/json”. If you use json format to send requests, you need to set this parameter to “application/json”.
6. Headers: Additional information of HTTP request headers, which can be an object used to set custom request headers.
7. timeout: Set the request timeout time in milliseconds. The default is 0, which means no timeout is set.
8. async: Whether to use asynchronous mode to send requests, the default is true.
9. beforeSend: function called before sending the request. This function can be used to modify the parameters before the request.
10. Complete: A function called after the request is completed. It will be called whether the request succeeds or fails.
11. success: function called after the request is successful. The callback function receives the data returned from the server as a parameter.
12. error: function called after the request fails. The callback function will receive HTTP error information as a parameter.
Complete code example
$.ajax({ url: "http://example.com/api/data", type: "POST", data: { name: "John", age: 30 }, dataType: "json", contentType: "application/json", headers: { "X-Auth-Token": "abcdef123456" }, timeout: 5000, async: true, beforeSend: function(xhr) { // Modify request headers or data before sending the request xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); }, complete: function(xhr, status) { // Processing logic after the request is completed console.log("Request completed"); }, success: function(response) { // Process successful response data console.log(response); }, error: function(xhr, status, error) { // Handle request failure console.log("Request failed:" + error); } });
1. url
: The URL address to which the request is to be sent.
url: “m/api/data”,
2. type
: request Type, optional parameters, including GET, POST, PUT, DELETE, etc. The default is GET.
Send a GET request:
$.ajax({ url: "http://example.com/api/data?id=1 & name=John", type: "GET", success: function(response) { // Process successful response data console.log(response); }, error: function(xhr, status, error) { // Handle request failure console.log("Request failed:" + error); } });Send POST request:
$.ajax({ url: "http://example.com/api/data", type: "POST", data: { name: "John", age: 30 }, success: function(response) { // Process successful response data console.log(response); }, error: function(xhr, status, error) { // Handle request failure console.log("Request failed:" + error); } });Send PUT request:
$.ajax({ url: "http://example.com/api/data", type: "PUT", data: { name: "John", age: 30 }, success: function(response) { // Process successful response data console.log(response); }, error: function(xhr, status, error) { // Handle request failure console.log("Request failed:" + error); } });Send DELETE request:
$.ajax({ url: "http://example.com/api/data", type: "DELETE", data: { id: 1 }, success: function(response) { // Process successful response data console.log(response); }, error: function(xhr, status, error) { // Handle request failure console.log("Request failed:" + error); } });
3. data
: To be sent The data to the server can be a string, array or object. Default is null.
data: “name=John & amp;age=30”,
data: [1, 2, 3, 4, 5],
data: { name: “John”, age: 30 },
4. dataType
: The type of response data returned from the server, optional parameters, including “xml”, “json”, “html “, “text”, etc., the default is intelligent guessing.
dataType: “json”,
dataType: “xml”,
dataType: “html”,
dataType: “text”,
5. contentType
: The data format for sending requests, optional parameters, the default is “application /x-www-form-urlencoded”. Another commonly used one is “application/json”. If you use json format to send a request, you need to set this parameter to “application/json”.
contentType: “application/json”,
contentType: “application/x-www-form-urlencoded”,
6. headers
: HTTP request The additional information of the header can be an object used to set custom request headers.
headers: { “X-Auth-Token”: “abcdef123456” },
7. timeout
: Set the request timeout. Time, unit is milliseconds, default is 0, that is, no timeout is set.
timeout: 5000,
8. async
: Whether to use asynchronous mode to send requests, the default is true.
async: true,
9. beforeSend
: Before sending the request function called. This function can be used to modify the parameters before the request.
beforeSend: function(xhr) {
// Modify request headers or data before sending the request
xhr.setRequestHeader(“X-Requested-With”, “XMLHttpRequest”);
},
10. complete
: The function called after the request is completed, regardless of Called whether the request succeeds or fails.
complete: function(xhr, status) {
// Processing logic after the request is completed
console.log(“Request completed”);
},
11. success
: After the request is successful function called. The callback function receives the data returned from the server as a parameter.
success: function(response) {
// Process successful response data
console.log(response);
},
12. error
: A function called after a request fails. function. The callback function will receive HTTP error information as a parameter.
error: function(xhr, status, error) {
// Handle request failure
console.log(“Request failed:” + error);
}