//define a way for the public to deal with Ajax functionHandelajax (url,method,parm,callback) {$.ajax ({url:url, Type:method, Datatyp E:' JSON ', Data:pram, success:function(data) {callback (data); }, Error:function(ERR) {alert (ERR); } }) }//Implementing QueriesHandelajax ('/user/search ', ' post ', {name: ' Wes '},function(data) {//Post-Success ActionsConsole.log (' Data: ', data); });
View Code
One of the reasons why Ajax jumps into the error function:
Any errors will jump to the error function;
Give an example of the cause of the error:
Problem with 1.URL path
There is no Chinese in the path
2. Problems with the passed parameters
The parameter must be an AJAX-supported encoding format, such as: With Json.parm (), json.stringify (), etc.
3.data cannot be empty
Pass a "{}" even if you do not pass the value
4.dataType
Background incoming datatype and foreground write inconsistencies
5.async Request Synchronous Asynchronous problem
Async Default Request method is true (asynchronous request), if you want an AJAX request to complete before requesting another Ajax, you need to set Async to false;
For example, you use a POST request to pass a value to another page background, but the page once loaded your Ajax has been executed, the value receive is completed in the background, the request is not the data, so you can consider the AJAX request to synchronize to try.
Reference to an online blog about the reasons why Ajax jumps into the error function
Ajax interface and background interaction