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

Analysis of some common properties and events of ajax objects and examples of common method case

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

Share

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

Some common properties and events of ajax objects and case analysis of methods are common examples. To solve this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Recently, I am quite free, so take the time to sort out some things about the ajax method. In the project, it is often found that many problems in the ajax section are properties, methods, and events that are not case-sensitive, which eventually leads to trouble in running the program.

Here are some common properties, events, and methods of the ajax object

1) the properties of standard ajax objects are readyState,status,responseText,responseXML

2) non-standard ajax object properties, for IE browsers, there are responseBody,2 binary data streams. If browser compatibility is not taken into account, this attribute + VBScript can solve the garbled problem very well.

Visual Basic code

Function Bytes2BStr (vin) 'binary conversion string strreturn = "" for I = 1 to lenb (vin) thischarcode = ascb (midb (vin,i,1)) if thischarcode < & h80 then strreturn = strreturn & chr (thischarcode) else nextcharcode = ascb (midb (vin,i+1,1)) strreturn = strreturn & chr (clng (thischarcode) * & h200 + cint (nextcharcode)) I = iTun1 end if next Bytes2BStr = strreturnEnd Function

3) event onreadystatechange, state transition function

4) the method has setRequestHeader,open,send, which is used to set the response header. When you specify the submission method as POST, you need to set content-type to application/x-www-form-urlencoded. If this response header is not set, the requested dynamic page cannot get the value in the form of key-value pairs, but the submitted data can be generated from the binary stream.

Referenc

How to get submitted content when ajax is specified as post but content-type is not set or key is not specified

Problems that are prone to mistakes

1) for objects created using XMLHttpRequest, including the IE7+, attribute is strictly case-sensitive, be sure to pay attention to the case problem, or not case-sensitive, the attribute value is undefined. For example, readyState is written as readystate,responseText and written as responsetext.

2) for IE browsers, if activexobject is judged first, activexobject will be executed to create xhr objects, and xhr objects, properties, events, and methods created by acx are not case-sensitive.

3) when creating with XMLHttpRequest, the onreadystatechange must be all lowercase, or it is equivalent to assigning a custom property to the xhr object, rather than the actual state transition function. This will never perform a callback. Case-insensitive xhr objects created by IE using activexobject

4) same as 2: 00 or 3: 00, when XMLHttpRequest is created, the calling method must be case-sensitive, or it will make an error and prompt that the method cannot be found. IE creation using activexobject is not case-sensitive.

In particular, the open method must be called before the setRequestHeader method can be executed, or there will be an error.

Some suggestions for handwritten ajax code

1) when submitting for get, there is no need to set content-type, unless the request is some file that needs to check content-type to generate some corresponding data format. For example, the webservice of asp.net will check the content-type. If you specify content-type as application/json, the corresponding string in json format will be generated.

2) when submitting for get, if there is no data, it is best to add a null as a parameter when calling the send method. Xhr.send (null)

3) when submitting for post, don't forget to call the open method and then call the setRequestHeader method to set content-type to application/x-www-form-urlencoded.

4) when using the responseText,responseXML and ResponseBoy [IE only], status attribute, you need to use it when readyState==4,status status = = 200 [online test] or status status = = 0 [local test]

JavaScript code

Var xhr = window.XMLHttpRequest? New XMLHttpRequest (): new ActiveXObject ("microsft.xmlhttp"); xhr.open ('get',' index.html', true); xhr.onreadystatechange = function () {if (4 = = xhr.readyState) {if (200 = = xhr.status | | 0 = = xhr.status) {/ / = normal returned processing code} else alert ('dynamic page has a problem ~');} xhr.send (null)

5) if it is executed asynchronously, you need to add a state transition function, and then use the responseText or responseXML attribute when readyState bit 4.

If you are synchronizing, you can use the responseText or responseXML property directly after the send, without adding the onreadystatechange state transition function. However, if the network speed is slow during synchronization, it is easy to fake the death of the browser and the user experience is not good.

6) switch to the jquery framework for the simplest hoho~~.

This is the answer to some common sample analysis questions about some common properties and events of ajax objects, as well as the case of methods. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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