Inheriting the previous 5 new jQuery.Ajax() examples (jQuery 1.9), I want to record the changes brought about by using the Promise interface of jQuery .Ajax() 1.9 (some of which can be classified as advantages).
success
-> done
, error
-> fail
, complete
, always
-> fail
The callback order is: 1. done
, 2. always
, 3. .always(), .always(), .always()
You can specify any number of callbacks of the same type. For example, .then()
Starting with jQuery 1.8, you can use the .done()
You can combine .fail()
and .then()
into // 旧的 complete 函数 complete Function(jqXHR jqXHR, String textStatus) // 新的 done 函数 jqXHR.done(function(data, textStatus, jqXHR) {});
Source: jQuery.Ajax API If you find that I have missed something else, feel free to comment.
done()
You can combine the fail()
and then()
functions into a
var promise = $.ajax({ url: "/myServerScript" }); promise.then(mySuccessFunction, myErrorFunction);
Source: Deferred and Promise in jQuery, chained Ajax request
then()
Starting with jQuery 1.8, you can call the promise1
functions in a chain order. In the following code, first run getStuff
, after successful parsing, run
var promise1 = $.ajax("/myServerScript1"); function getStuff() { return $.ajax("/myServerScript2"); } promise1.then(getStuff).then(function(myServerScript2Data) { // 两个 Promise 都已解析 });
Each callback function will receive the result of the previous asynchronous function. For Ajax, that is the returned data.
.when()
You can use .done()
to assign a Promise callback function, i.e.
var container = $("#mycontainer"); $.when( function () { return $.Deferred(function (dfd) { container.fadeOut('slow', dfd.resolve); }).promise(); }(), $.ajax({ url: 'Path/To/My/Url/1', type: 'POST', dataType: 'json' }), $.ajax({ url: 'Path/To/My/Url/2', type: 'POST', dataType: 'json' }) ).done(function (x, data) { container.html('Your request has been processed!'); container.fadeIn('slow'); });
As you can see, we pass three promises to $.when, one for fade out animation and two for Ajax operations.
dfd.resolve
function is passed to the callback parameter of fadeOut()
, which means that once the animation is completed, Deferred will be parsed. Source: Always keep your (jQuery) Promise, FAQs about the advantages of jQuery 1.9 AJAX Promise interface
The main advantage of using the jQuery 1.9 AJAX Promise interface is that it can handle multiple AJAX requests simultaneously. This feature is especially useful when you need to perform multiple AJAX requests and want to perform certain operations only after all requests have completed. The Promise interface provides a method to aggregate multiple AJAX request results and execute callback functions until all requests are completed. This makes the code easier to read and manage.
(The answers to the following questions are the same as the original text, but the wording has been slightly adjusted to maintain the original text.)
jQuery 1.9 AJAX Promise interface improves error handling by providing a unified way to handle errors. Without writing separate error handling code for each AJAX request, you can use the Promise interface to handle all errors in one place. This not only simplifies the code, but also makes the code easier to maintain and debug.
Yes, you can use the jQuery 1.9 AJAX Promise interface with other versions of jQuery. However, note that the Promise interface was introduced in jQuery 1.5, so it does not work with earlier versions. Additionally, some features of the Promise interface may not be available in later versions of jQuery, so it is always recommended to check the jQuery documentation for compatibility issues.
The jQuery 1.9 AJAX Promise interface has several advantages over other methods that handle AJAX requests. It provides a more structured and organized way to handle multiple AJAX requests, improves error handling, and makes the code easier to read and maintain. However, for beginners, it may be a little more complicated than other methods.
The Promise and Deferred objects in jQuery are both used to manage asynchronous operations, but they have different uses. The Deferred object represents an unfinished unit of work, while the Promise object represents the final result of the work. In other words, a Deferred object can be parsed or rejected, while a Promise object can only be fulfilled or rejected.
Converting a traditional AJAX call to a Promise-based AJAX call involves wrapping an AJAX call in a function that returns a Promise object. The Promise object is then parsed or rejected based on the success or failure of the AJAX call.
Yes, you can use the jQuery 1.9 AJAX Promise interface with other JavaScript libraries. However, you need to be aware of potential compatibility issues, especially if other libraries also use Promise or similar constructs.
You can use the $.when() function to handle multiple AJAX requests using the jQuery 1.9 AJAX Promise interface. This function takes multiple Promise objects as parameters and returns a new Promise object that resolves when all input Promise objects are parsed.
If an AJAX request fails when using the jQuery 1.9 AJAX Promise interface, the Promise object associated with the request will be rejected. You can handle this by attaching the .fail()
handler to the Promise object, which will be called if the Promise is rejected.
Yes, you can use the jQuery 1.9 AJAX Promise interface for non-AJAX asynchronous operations. The Promise interface is a general construct for managing asynchronous operations, so it can be used with any operation that may not be done immediately, such as reading files or querying databases.
The above is the detailed content of jQuery 1.9 .Ajax() New Promise Interface Advantages. For more information, please follow other related articles on the PHP Chinese website!