error usage: 1. Used when the dataType type returned by the background is inconsistent with what is written in the foreground; 2. Used when async requests synchronization and asynchronous problems; 3. Used when data is set to empty; 4. Used when the passed parameters are not in the encoding format supported by ajax.
The operating environment of this article: windows7 system, javascript1.8.5&&html5 version, Dell G3 computer.
When is error used in ajax?
dataType error (dataType is used to specify background return parameters Type)
# Type error: If the dataType type returned by the background is inconsistent with what is written in the front desk, an error will be jumped.
Format error: After jquery1.4, the format requirements for json are very strict, and json format errors will also jump into error.{"test":1} Pay attention to the format
Sometimes, when it is not needed In the case of returning a value, follow the template format and set the dataType: "json" parameter; at this time, when the ajax value is transferred correctly, there will be a special case of an error being reported in the 200 return success status.
If not specified, jQuery will automatically make intelligent judgments based on the MIME information of the HTTP package. For example, the XML MIME type is recognized as XML. In 1.4, JSON will generate a JavaScript object, and script will execute the script. The data returned by the server will then be parsed based on this value and passed to the callback function. Available values:
"xml": Returns an XML document that can be processed with jQuery.
"html": Returns plain text HTML information; the included script tag will be executed when inserted into the dom.
"script": Returns plain text JavaScript code. Results are not cached automatically. Unless the "cache" parameter is set. Note: When making remote requests (not under the same domain), all POST requests will be converted into GET requests. (Because the DOM script tag will be used to load)
"json": Returns JSON data.
"jsonp": JSONP format. When calling a function using JSONP, such as "myurl?callback=?" jQuery will automatically replace ? with the correct function name to execute the callback function.
"text": Returns a plain text string
async request synchronous asynchronous problem
Async defaults to true (asynchronous request). If you want to execute another Ajax after one Ajax is executed, you need to set async=false
. For example, you use a post request to pass a value to another page background, but the page Loading your ajax has already been executed, and the value transfer and reception is completed in the background. At this time, the data cannot be requested, so you can consider changing the ajax request to synchronous try.
data must be written
#If data is empty, "{}" must be passed; otherwise, the returned xml format. And prompts parsererror. data:”{}”
The exception of parsererror is also related to the Header type. And encoding header('Content-type: text/html; charset=utf8');
Parameters passed
Must be an encoding format supported by ajax
##URL path problem
3 - (Interaction) Parsing the response content 4 - (Complete) The response content parsing is completed and can be called on the client XMLHttpRequest.status: Call http request statusThere are many request statuses. If you encounter specific error status codes, you can check them on Baidu. XMLHttpRequest.responseText: Returned error messageIf an error occurs, the error message (second parameter) may not only be null, but also may be "timeout", "error", "notmodified" and "parsererror". [Related tutorial recommendations:
AJAX video tutorial]
The above is the detailed content of When is error used in ajax?. For more information, please follow other related articles on the PHP Chinese website!