jquery is very powerful and convenient in terms of Ajax, and here's how jquery makes Ajax requests when it comes to the method template:
- $. Ajax({
- type: "get",
- URL: " ",
- Data : {},
- beforesend : function() {
- },
- Success : function(data) {
- },
- Complete : function() {
- }
- });
The use of the $.ajax () method is not the purpose of the Ming River writing this article, today the Ming River mainly explains the complete event flow when making Ajax requests.
All global events for the Ajax method of jquery:
- Ajaxstart:ajax before the request begins
- Ajaxsend:ajax when requested
- Ajaxsuccess:ajax after getting the data
- Ajaxcomplete:ajax when the request is complete
- Ajaxerror:ajax after an error has occurred on the request
- Ajaxstop:ajax after a request is stopped
When you use jquery's ajax approach, both $.ajax (), $.get (), $.load (), $.getjson (), and so on, will trigger global events by default, but they are usually not bound to global events, but these global events are actually very useful.
The use of global events for AJAX methods
Ajax global events, there is a typical application scenario:
There are several or even a few AJAX requests on your page, but these AJAX requests have the same message mechanism. A prompt is displayed before the AJAX request begins, prompting "reading data", and the Prompt box displays "Data acquisition success" when the Ajax request succeeds, and hides the prompt after the AJAX request ends.
The practice of not using global events is:
Add the Beforesend, success, and complete callback functions to $.ajax (), and add the processing hints to the callback function.
The practice of using global events is:
- $(document). Ajaxstart(onStart)
- . ajaxcomplete(oncomplete)
- . ajaxsuccess(onsuccess);
- function onStart(event){
- //.....
- }
- function oncomplete(event,xhr, settings){
- //.....
- }
- function onsuccess(event,xhr, settings){
- //.....
- }
Ajax method Complete Event flow
For more intuitive explanation, Ming River use axure draw two flow chart, draw bad also please forgive, O (∩_∩) o