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

What are the reasons for ajax error?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the causes of ajax error for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The reasons for ajax error: 1, wrong dataType type; 2, wrong json format; 3, async request synchronous and asynchronous setting problem; 4, data parameter value is not set; 5, there is a problem with the encoding format of passing parameters; 6, Chinese is in the URL path.

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

A standard ajax code for jquery:

$.ajax ({type: 'POST', url:' getSecondClassification', data: {"sort2": sort2, "sort3": sort3)}, dataType: 'json', / / contentType: "utf-8". The encoding method for sending requests / / contentType is the default value: application/x-www-form-urlencoded contentType:' application/json' Data: JSON.stringify ({a: [{blv 1, av 1}]}) success: function (sort) {$("# noneSelect") .remove () Var optionstring = "; for (var i in sort) {optionstring + =" + sort [I] .className + ";} $(" # secondtype ") .html (optionstring); $(" # build ") .removeAttr (" disabled ") }, error: function (XMLHttpRequest, textStatus, errorThrown) {/ / $("# p_test") [xss_clean] = "there is something wrong!"; / / alert (XMLHttpRequest.status); / / alert (XMLHttpRequest.readyState); / / alert (textStatus);}})

Any error will jump to the error function.

Give examples of some of the causes of errors:

1. DataType error

Type error: the inconsistency between the dataType type returned by the background and the one 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.

2. 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.

3. 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')

4. Passed parameter

Must be an encoding format supported by ajax

5. URL path problem

Path cannot be in Chinese

Debug error messages with error:

You can alert out to see exactly what went wrong.

Specific parameters:

XMLHttpRequest:XMLHttpRequest.readyState: meaning of status code 0-(uninitialized) has not yet called send () method 1-(load) called send () method, sending request 2-(load complete) send () method execution completed, has received all response content 3-(interaction) parsing response content 4-(completion) response content parsing is complete, can be called on the client side

XMLHttpRequest:XMLHttpRequest.status:

TextStatus: cause of error

ErrorThrown: (optional) captured error object

This is the end of this article on "what are the causes of ajax error". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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