In jQuery, commonly used AJAX request methods include GET requests and POST requests. AJAX (Asynchronous JavaScript and XML) is a technology that asynchronously initiates HTTP requests and updates pages through JavaScript. It can send requests to the server and obtain data without refreshing the entire page. The following will introduce specific code examples for GET requests and POST requests respectively.
$.get('https://api.example.com/data', function(data) { // 请求成功后的回调函数 console.log(data); }).fail(function() { // 请求失败时的回调函数 console.log('请求失败'); });
In the above code, we use the $.get method to send a GET request to 'https://api.example.com/data', and print the return when successful data, and output "Request failed" when it fails.
var postData = { name: 'Alice', age: 25 }; $.post('https://api.example.com/save', postData, function(data) { // 请求成功后的回调函数 console.log(data); }).fail(function() { // 请求失败时的回调函数 console.log('请求失败'); });
In the above code, we use the $.post method to send a POST request to 'https://api.example.com/save' and also send the postData object as request data. Print the returned data when successful, and output "Request Failed" when failed.
In addition to the above two commonly used request methods, jQuery also provides the $.ajax method to implement more flexible AJAX requests. You can customize the request method, request header, data format, etc. By flexibly using these methods, rich and diverse front-end and back-end interactions can be achieved.
The above is the detailed content of What are the commonly used AJAX request methods in jQuery?. For more information, please follow other related articles on the PHP Chinese website!