Ajax and some garbled issues

Source: Internet
Author: User

Create an xmlhttprequest object.

Copy codeThe Code is as follows: return window. ActiveXObject? New window. ActiveXObject ('Microsoft. xmlhttp'): new XMLHttpRequest;
The next object is an onreadystatechange event. There are two attributes: readyState and status. Simple AJAX we will use these things.
The createXMLHttp () method used below is the above Code!
1: implement a request in GET mode;

Copy codeThe Code is as follows:
Var get = function (url, B, callback ){
Var xmlhttp = createXMLHttp ();
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 & xmlhttp. status = 200 ){
Callback (xmlhttp. responseText );
}
}
If (B! = Undefined ){
Var arr = [], e;
For (e in B ){
Arr. push (e + '=' + encodeURIComponent (B [e]);
// Arr. push (e + '=' + B [e]);
// If the encodeURIComponent encoding is not used, Chinese characters cannot be correctly transmitted in IE8.
}
Url + = '? '+ Arr. join ('&');
}
Xmlhttp. open ('get', url, true );
Xmlhttp. send ();
}

// The parameter settings of this function can also be set according to your own habits.

Use GET to send a request. We will format the parameters to be passed (a = 1, B = 2) and then append them to the end of the URL.
The server page can be obtained by obtaining URL parameters. (For example, Php: $ _ GET ["a"]);
It is worth noting that an encodeURIComponent () method is used for encoding when formatting parameters to avoid garbled characters;
In fact, there are three methods to complete this job. Escape, encodeURI, and encodeURIComponent. You can check the information. The first two methods are not encoded for some special characters.
Therefore, using 3rd is a good choice.
If you do not use encoding to send data directly, different browsers may behave differently. For example, Internet Explorer may cause garbled characters when sending Chinese data (of course, there are still many garbled characters. Please refer to it later ..).

2: Implementation of sending a request using the POST method
Copy codeThe Code is as follows:
Var ajax = function (){
Var xmlhttp = createXMLHttp ();
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 & xmlhttp. status = 200 ){
A. success (xmlhttp. responseText );
} Else return xmlhttp;
}
Xmlhttp. open ('post', a. url, true );
// Header information must be set when the request is post
Xmlhttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded ");
// Serialize the data to be sent
Var c = [];
For (var e in a. data ){
C. push (e + '=' + encodeURIComponent (a. data [e]);
// After URL encoding is tested, the correctness of the sent data can be ensured.
// Some special characters may not be correctly sent due to unencoding
}
A. data = c. join ('&');
Xmlhttp. setRequestHeader ("Content-length", c. length); // optional
Xmlhttp. setRequestHeader ("Connection", "close"); // optional
Xmlhttp. send (a. data );
// After the data is sent, the server uses post to obtain the data, for example, php $ _ POST ['a'];
}

Parameter a is an object that contains {url: "http:...", data: {a: 1, b2}, success: function (){}}
Callback Function for successful address data
Use the POST method to send a request. The data must also be formatted (a = 1, B = 2). However, unlike the GET method, we write the data in the send () method (xmlhttp. send (. data );).
On the server page, you can obtain the form data. (For example, Php: $ _ POST ["a"]);
It is worth noting that when using post to send a request, we must set the request header information.
Xmlhttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded ");
If the Content-type is not set to application/x-www-form-urlencoded, the data sent by the send () method cannot be obtained on the server page.

Finally, we will discuss the Garbled text. There are already two errors.
1: for example, if the parameter is not encoded for direct transfer, the transfer will fail.
2: Content-type is not set when the POST method is used. The server page cannot obtain the sent parameters;
3: encoding of the sending request page and request page. In a standard browser, make sure that the encoding method for the request page and the requested page is UTF-8. Otherwise, Chinese characters will be miserable.
Copy codeThe Code is as follows:
<! -- <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> -->
<! -- // After testing, the above meta statement charset will be garbled in IE without being used -->
<! --
* Header ("Content-Type: text/html; charset = gb2312 ");
The called AJAX page uses gb2312 encoding and garbled characters appear under chorme.
Normal access in IE
-->


The above Garbled text is very messy and cannot be ruled out. So keep the two page Encoding As UTF-8 parameter encoding before passing. Can effectively prevent garbled characters
Several file packaging http://xiazai.jb51.net/201008/yuanma/ajax_php.rar I tested

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.