ajax objects common properties, events, methods sensitive issues

Here are some common properties, events and methods ajax object
  1) standard attribute ajax object have the readyState, Status, the responseText, the responseXML

  2) ajax nonstandard object properties, for IE browser, there responseBody, 2 binary data stream . If you do not consider browser compatibility, this property + VBScript good solution to the garbage problem.  
Basic code Visual
?
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
[size=1em]Function  [size=1em]Bytes2BStr(vin) [size=1em]'二进制转字串
[size=1em]      [size=1em]strreturn =  [size=1em]""
[size=1em]      [size=1em]for i = 1 to lenb(vin)
[size=1em]      [size=1em]thischarcode = ascb(midb(vin,i,1))
[size=1em]      [size=1em]if thischarcode < &h80 then
[size=1em]         [size=1em]strreturn = strreturn & chr(thischarcode)
[size=1em]      [size=1em]else
[size=1em]         [size=1em]nextcharcode = ascb(midb(vin,i+1,1))
[size=1em]         [size=1em]strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode))
[size=1em]         [size=1em]i = i + 1
[size=1em]      [size=1em]end if
[size=1em]      [size=1em]next
[size=1em]      [size=1em]Bytes2BStr = strreturn
[size=1em]End  [size=1em]Function
 





  3) the onreadystatechange event, the state transition function

  4) methods setRequestHeader, open, send, for setting in response to the head, when the specified filed POST method, it is necessary to set the content-type application / x-www-form- urlencoded, If the response header is not provided, the requested dynamic page can not be used in the form of key-value pairs of values acquired, but may be generated from the data submitted in the binary stream.
Reference
ajax designated as the post but did not set the content-type or specify how to obtain keys from time to time submissions

  error prone to problems
1) for the use of XMLHttpRequest objects created, including IE7 +, property is strictly case-sensitive, must pay attention to size written questions, or insensitive, the property value is undefined. As readyState written readystate, responseText written responsetext.

2) For the IE browser, if it is judged activexobject first, then executes activexobject create xhr objects created using acx xhr objects, properties, events, methods are case-insensitive

3) using XMLHttpRequest creation, onreadystatechange necessarily all lowercase , or is equivalent to an object xhr assign a custom property, and not the actual state transition function. So never perform the callback. For IE uses the object activexobject xhr created, not case-sensitive

4) with 2,3 points, when XMLHttpRequest is created, call the method must be case-sensitive and should not go wrong, suggesting no method. IE uses to create activexobject not case-sensitive.
  Special emphasis is, be sure to call the open method, to perform setRequestHeader method, you should not go wrong.

  Code recommendations handwritten ajax
1) is submitted to get, no need to provide a content-type, unless the request is to check some of the content-type to generate some corresponding data format. The webservice asp.net checks the content-type, if the specified content-type is application / json, json format corresponding to the character is generated.
Reference WebService JSON

2) submitting to get, if there is no data, when calling the send method is preferably added as a null argument. xhr.send (null);

3) the submission to post, must not forget that after calling the open method, then call setRequestHeader method to set the content-type to the Application / the X-form-urlencoded-the WWW-

4) using responseText, responseXML and in responseBody [IE only], when the status property, you need to readyState == 4, status == 200 [state] test or online status status == 0 [test] when local re-use

JavaScript code
?
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
[size=1em]       [size=1em]var  [size=1em]xhr = window.XMLHttpRequest ?  [size=1em]new  [size=1em]XMLHttpRequest() :  [size=1em]new  [size=1em]ActiveXObject( [size=1em]"microsft.xmlhttp" [size=1em]);
[size=1em]       [size=1em]xhr.open( [size=1em]'get' [size=1em],  [size=1em]'index.html' [size=1em],  [size=1em]true [size=1em]);
[size=1em]       [size=1em]xhr.onreadystatechange =  [size=1em]function  [size=1em]() {
[size=1em]           [size=1em]if  [size=1em](4 == xhr.readyState) {
[size=1em]               [size=1em]if  [size=1em](200 == xhr.status || 0 == xhr.status) {
[size=1em]                   [size=1em]//=========正常返回后的处理代码
[size=1em]               [size=1em]}
[size=1em]               [size=1em]else  [size=1em]alert( [size=1em]'动态页出问题了~~' [size=1em]);
[size=1em]           [size=1em]}
[size=1em]       [size=1em]}
[size=1em]       [size=1em]xhr.send( [size=1em]null [size=1em]);
 






5) If the execution is asynchronous, it is necessary to add the state transition function, and then use the property responseText when readyState responseXML or 4 position.
If synchronization is performed, after you can send, directly or responseText responseXML property, without adding onreadystatechange the state transition function. But if the synchronization is likely to cause Suman browser suspended animation, the user experience is not good.
Published 965 original articles · won praise 11 · views 30000 +

Guess you like

Origin blog.csdn.net/xiaoyaGrace/article/details/105285396