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 deal with the mistakes made by Ajax

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

Share

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

This article will explain in detail how to deal with the mistakes made by Ajax. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. There is a problem with the if else statement when ajax sends the request.

Var xhr = new XMLHttpRequest (); xhr.open ("get", strUrl)

Xhr.onreadystatechange = function () {

If (xhr.readyState = 4) {

If (xhr.status = = 200) {console.log (xhr.responseText);}

} else {

Console.log (xhr.status);}

} xhr.send ()

At that time, two 200 status codes were output, and then the xhr.responseText was output to get the content. It was very strange at that time. I don't know how these two 200s came from.

At that time, I tried to output a lot of content, but I still couldn't find the reason. The problem was not found until the else statement was changed to console.log (xhr.readystate), and then the output result check was 2p3, and the obtained content was in turn. Only then did I find out the reason:

When xhr sends a request to the end of the request, it will go through five statuses. The status code is 0-readystate 1-2-3-4 (that is, the value of readystatechange). Every time the status changes, the readystatechange event will be triggered and the event handler function will be called. When the status code is changed to 2, it calls the readystatechange event and then goes to the if/else statement

This is readystate, obviously not 4, and then go to the else statement to output 2. And so on, when the readystatechange event is called at 3. 4, the content in the if will be taken only at 4: 00. The request succeeds and the request content is returned.

This is the end of this article on "how to deal with mistakes made by Ajax". I hope the above content can be of some help 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