Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use error in ajax

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to use error in ajax. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Error usage: 1, when the dataType type returned by the background is inconsistent with that written in the foreground; 2, when the async requests synchronous and asynchronous problems; 3, when the data is set to empty; 4, when the passed parameters are not in the encoding format supported by ajax.

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

When is error used in ajax

DataType error (dataType is used to specify the type of parameter returned by the background)

Type error: the inconsistency between the dataType type returned by the background and that written by the foreground will jump into error.

Format errors: after jquery1.4, the format requirements of json are very strict, and json format errors will jump into error. {"test": 1} pay attention to the format

Sometimes, when the return value is not needed, the dataType: "json" parameter is set by the template format; in this case, when the ajax value is passed correctly, there is a special case of an error report in the successful state.

If not specified, jQuery automatically determines intelligently based on the MIME information of the HTTP package, such as the XML MIME type is recognized as XML. In 1. 4, JSON generates a JavaScript object, and script executes the script. The data returned by the server is then parsed according to this value and passed to the callback function. Available values:

"xml": returns the XML document, which can be processed with jQuery.

"html": returns plain text HTML information; the included script tag is executed when the dom is inserted.

"script": returns plain text JavaScript code. The results are not automatically cached. Unless the cache parameter is set. Note: when making remote requests (not in the same domain), all POST requests will be converted to GET requests. (because the script tag of DOM will be used to load)

"json": returns JSON data.

"jsonp": JSONP format. When calling a function in the form of JSONP, such as "myurl?callback=?" Will jQuery be replaced automatically? Is the correct function name to execute the callback function.

"text": returns a plain text string

Synchronous and asynchronous problem of async request

Async defaults to true (asynchronous request). If you want to execute an Ajax and then execute another Ajax, you need to set the async=false

For example, you use post request to send a value to another page backend, but your ajax has already been executed as soon as the page is loaded, and the value is received in the background, so no data can be requested at this time, so you can consider changing the ajax request to synchronization.

Data has to write.

If data is empty, be sure to pass "{}"; otherwise, the return is in xml format. And prompt parsererror. Data: "{}"

The exception of parsererror is also related to the Header type. And coding header ('Content-type: text/html; charset=utf8')

Passed parameter

Must be an encoding format supported by ajax

URL path problem

Path cannot be in Chinese

Generally speaking, we can determine the cause of the error by analyzing some parameters in error:

XMLHttpRequest.readyState: status code

0-(uninitialized) the send () method has not been called yet

1-(load) has called the send () method and is sending the request

2-(load completed) the send () method has been executed and all the response contents have been received.

3-(interactive) parsing response content

4-(complete) the parsing of the response content is complete and can be called on the client side.

XMLHttpRequest.status: call http request status

There are many request status, specific error status code can be queried by Baidu.

XMLHttpRequest.responseText: error message returned

If an error occurs, the error message (the second parameter) may be "timeout", "error", "notmodified" and "parsererror" in addition to getting null.

After reading the above, have you mastered how to use error in ajax? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report