Ajax and some garbled problems _ajax related

Source: Internet
Author: User
Creates a XMLHttpRequest object.

Copy Code code as follows:
return window. ActiveXObject? New window. ActiveXObject (' Microsoft.XMLHTTP '): New XMLHttpRequest;

A onreadystatechange event for the secondary object. There are two attributes Readystate,status. Simple Ajax We're going to use these few things.
The following createxmlhttp () method used is the above code!
1: The implementation of a request is issued in Get mode;

Copy Code code 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]);
Tested if Chinese is not delivered correctly under IE8 without using encodeURIComponent encoding
}
url = '? ' + arr.join (' & ');
}
Xmlhttp.open (' Get ', url, true);
Xmlhttp.send ();
}

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

To send a request using a Get method, we append the parameter to be passed (a=1,b=2) to the back of the URL.
The server page is available using the method of obtaining the URL parameters. (For example, PHP: $_get["a"]);
It is noteworthy that we use a encodeuricomponent () method to encode the parameters in order not to appear garbled;
There are actually 3 ways to do this work. Escape,encodeuri,encodeuricomponent, we can check the data. The first two methods are not encoded for some special characters.
So use the 3rd one is a better choice.
If you send data directly without coding, the performance of each browser may be different. For example, ie you send Chinese data will appear garbled (of course, there is a lot of garbled situation, please continue to see. )。

2: Send a request by POST method implementation
Copy Code code as follows:

var ajax = function (a) {
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");
Serializing the data to be sent
var c = [];
for (Var e in a.data) {
C.push (e + ' = ' + encodeURIComponent (a.data[e]));
After the test through the URL coding more to ensure the correctness of the data sent.
Non-encoding may cause some special characters to not be sent correctly
}
A.data = C.join (' & ');
Xmlhttp.setrequestheader ("Content-length", c.length); It seems to be optional.
Xmlhttp.setrequestheader ("Connection", "close"); It seems to be optional.
Xmlhttp.send (A.data);
After the data is sent, the server uses post to obtain data such as PHP $_post[' a '];
}

Parameter A is an object that he contains {URL: "http: ...", Data:{a:1,b2},success:function () {}}}
Address Data Success callback function
Use the Post method to issue the request. Data must also be formatted (a=1,b=2); But unlike get methods, we write the data in the Send () method (Xmlhttp.send (A.data)).
The server page can be obtained by using the form data. (For example, PHP: $_post["a"]);
It is important to note that when sending a request using post, we must set the header information for the request.
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
After testing if Content-type is not set to application/x-www-form-urlencoded, the server page is unable to get the data sent through the Send () method.

Finally on the problem of garbled. There are already two places where there are mistakes.
1: For example, the parameters do not encode direct transmission, there will be transmission of unsuccessful problems.
2: When using the Post method is not set Content-type, the server page can not get the parameters sent over;
3: Send the request page and the request page coding problem. In standard browsers please be sure to keep the request page and the requested page encoding is utf-8, otherwise the Chinese will be very tragic.
Copy Code code as follows:

<!--<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>-->
<!--//tested not using the above Meta declaration charset in IE will appear garbled-->
<!--
* Header ("content-type:text/html; charset=gb2312 ");
The invoked Ajax page uses the GB2312 encoding to appear garbled under Chorme.
Normal access under IE
-->


The above here appear garbled situation is very messy, not easy to exclude. So keep two pages encoded as UTF-8 parameters after passing. can effectively prevent garbled
I tested several files to pack Http://xiazai.jb51.net/201008/yuanma/ajax_php.rar
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.