Summary of common attributes, events, and methods of ajax objects in Case sensitivity.

Source: Internet
Author: User

Summary of common attributes, events, and methods of ajax objects in Case sensitivity.

Recently it was relatively idle, so I took some time to sort out some ajax methods. In projects, it is often found that many problems of the ajax section are attributes, methods, and case-insensitive events, resulting in trouble in running the program.

  The following are some common attributes, events, and methods of ajax objects.

1) Standard ajax object attributes include readyState, status, responseText, responseXML

2) Non-standard ajax object attributes. for IE browsers, there are responseBody and binary data streams. If browser compatibility is not considered, this attribute + VBScript can solve the Garbled text problem.

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) * & h100 + cint (nextcharcode) I = I + 1 end if next Bytes2BStr = strreturnEnd Function

3) event onreadystatechange, status Conversion Function

4) methods include setRequestHeader, open, and send, which are used to set the response header. When the specified submission method is 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 be obtained using a key-value pair, however, the submitted data can be generated from the binary stream.

Reference

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

  Prone to errors

1) for objects created using XMLHttpRequest, including IE7 +, the attributes are case-sensitive. Pay attention to the Case sensitivity issue. Otherwise, the attribute value is undefined. For example, readyState is written as readystate and responseText is written as responsetext.

2) in IE browser, if activexobject is judged first, activexobject will be executed to create an xhr object. The xhr object, attributes, events, and methods created using acx are case insensitive.

3) when using XMLHttpRequest to create an object, onreadystatechange must all be in lower case, or assign a custom attribute to the xhr object instead of the actual State Conversion Function. In this way, the callback will never be executed. Xhr objects created by IE using activexobject are case-insensitive.

4) at, XMLHttpRequest must be case-sensitive when creating an XMLHttpRequest. If no error occurs, the system prompts that the method cannot be found. Using activexobject to create IE is case insensitive.

It is particularly emphasized that the setRequestHeader method must be called before execution.

  Some Suggestions on handwritten ajax code

1) You do not need to set the content-type when submitting a get request, unless you need to check the content-type to generate some corresponding data format files. For example, asp.net's webservice checks content-type. If the specified content-type is application/json, a corresponding json string is generated.

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

3) when submitting a post request, do not 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 responseBody [IE only], status attribute, you need, status = 200 online test or 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) {// ========== processing code after normal return} else alert ('dynamic page has a problem ~~ ') ;}} Xhr. send (null );

5) for asynchronous execution, you need to add a State Conversion Function and use the responseText or responseXML attribute when readyState is 4.
For synchronous execution, you can directly use the responseText or responseXML attribute after sending. You do not need to add the onreadystatechange status conversion function. However, if the network speed is slow during synchronization, the browser may be suspended and the user experience is poor.

6) simplest ~~ Hoho ~~ Use the jquery framework instead.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.