Ajax and some garbled code problems

Time:2021-11-29

Create an XMLHttpRequest object.

Copy codeThe code is as follows:
return window.ActiveXObject ? new window.ActiveXObject(‘Microsoft.XMLHTTP’) : new XMLHttpRequest;

An onreadystatechange event occurs for the secondary object. There are two properties readyState and status. Simple ajax, we will use these things.
The createxmlhttp () method used below is the above code!
1: Implementation of sending 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]);
//After testing, if you do not use encodeuricomponent coding, you cannot correctly transfer Chinese under IE8
}
url += ‘?’ + arr.join(‘&’);
}
xmlhttp.open(‘GET’, url, true);
xmlhttp.send();
}

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

To send a request using get, we format the parameters to be passed (a = 1, B = 2) and attach them to the back of the URL.
The server page can be obtained by obtaining the URL parameters. (for example, PHP: $_get [“a”]);
It is worth noting that we use an encodeuricomponent () method to encode the parameters in order to avoid garbled code;
There are actually three ways to do this. Escape, encodeuri, encodeuricomponent, you can check the data. The first two methods still do not encode some special characters.
So using the third is a better choice.
If you send data directly without encoding, the performance of each browser may be different. For example, when you send Chinese data in ie, there will be garbled code (of course, there are still many cases of garbled code, please continue to see…).

2: Implementation of sending a request by post method

Copy codeThe code is 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”);
//Serialize data to send
var c = [];
for(var e in a.data){
c.push(e + ‘=’ + encodeURIComponent(a.data[e]));
//After testing, after URL coding, it can better ensure the correctness of the sent data.
/ / failure to encode may cause some special characters to be sent incorrectly
}
a.data = c.join(‘&’);
xmlhttp.setRequestHeader(“Content-length”, c.length); // It seems optional
xmlhttp.setRequestHeader(“Connection”, “close”); // It seems 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 contains {URL: “http:…”, data: {A: 1, B2}, success: function() {}}
Address data successful callback function
Make a request using the post method. Data must also be formatted (a = 1, B = 2); However, unlike the get method, we write the data in the send () method (xmlhttp. Send (a.data);).
The server page can be obtained by obtaining 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.
xmlhttp.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
After testing, if the content type is not set to application / x-www-form-urlencoded; The server page cannot get the data sent through the send () method.

Finally, the problem of garbled code. There are already two mistakes in the above.
1: For example, if parameters are passed directly without coding, there will be a problem of unsuccessful transmission.
2: When using the post method, the content type is not set, and the server page cannot obtain the sent parameters;
3: The coding problem between the sending request page and the request page. In the standard browser, please make sure that the encoding of the requested page and the requested page is UTF-8, otherwise Chinese will be very tragic.

Copy codeThe code is as follows:
<!–<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />–>
<!–// After testing, if the above meta declaration charset is not used, garbled code will appear in IE — >
<!–
* header(“Content-Type: text/html; charset=gb2312”);
The called ajax page uses GB2312 coding, and there is garbled code under chorme.
Normal access under IE
–>

The situation of garbled code here is very chaotic, which is difficult to eliminate. Therefore, keep the two page codes as UTF-8 parameter codes before passing. It can effectively prevent garbled code
Several files I tested were packagedhttp://xiazai.jb51.net/201008/yuanma/ajax_php.rar

Recommended Today

On the mutation mechanism of Clickhouse (with source code analysis)

Recently studied a bit of CH code.I found an interesting word, mutation.The word Google has the meaning of mutation, but more relevant articles translate this as “revision”. The previous article analyzed background_ pool_ Size parameter.This parameter is related to the background asynchronous worker pool merge.The asynchronous merge and mutation work in Clickhouse kernel is completed […]