In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of some garbled code problems in ajax, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Create a xmlhttprequest object.
The copy code is as follows:
Return window.ActiveXObject? New window.ActiveXObject ('Microsoft.XMLHTTP'): new XMLHttpRequest
An onreadystatechange event for the secondary object. There are two properties, readyState,status. We will use these things in a simple AJAX.
The createXMLHttp () method used below is the above code!
1: implementation of issuing a request in GET
The copy 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])
/ / it has been tested that Chinese cannot be transmitted correctly under IE8 without using encodeURIComponent coding.
}
Url + ='? + arr.join ('&')
}
Xmlhttp.open ('GET', url, true)
Xmlhttp.send ()
}
/ / the specific parameter settings of this function can also be set according to your own habits.
To send a request using GET, we later append the parameter to be passed in the format (axi1recom bread2) to the end of URL.
The server page can be obtained by getting the URL parameter. (for example, Php: $_ GET ["a"])
It is worth noting that when we format the parameters, we use an encodeURIComponent () method to encode in order to avoid garbled code.
In fact, there are three ways to accomplish this task. Escape,encodeURI,encodeURIComponent, you can check the information. The first two methods are still not encoded for some special characters.
So using the third is a better choice.
If you send data directly without using encoding, the performance of each browser may be different. For example, IE, when you send Chinese data, there will be garbled code (of course, there are still many cases of garbled code, please continue to see..).
2: the implementation of sending a request in the POST method
The copy 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 the data to be sent
Var c = []
For (var e in a.data) {
C.push (e +'='+ encodeURIComponent (a.data [e]))
/ / after testing, the correctness of the transmitted data can be better ensured after URL coding.
/ / failure to encode may cause some special characters not to be sent correctly
}
A.data = c.join ('&')
Xmlhttp.setRequestHeader ("Content-length", c.length); / / seems to be optional
Xmlhttp.setRequestHeader ("Connection", "close"); / / seems optional
Xmlhttp.send (a.data)
/ / after the data is sent, the server uses post to obtain data such as php $_ POST ['a']
}
Parameter an is an object, which contains {url: "http:...", data: {avell 1direction b2}, success:function () {}}.
Address data successful callback function
Use the POST method to make the request. The data must also be formatted (adat1 a.data 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 getting the form data. (for example, Php: $_ POST ["a"])
It is worth noting that we have to set the header information of the request when using post to send the request.
Xmlhttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded")
It has been tested that the data sent through the send () method cannot be obtained if the Content-type is not set to the application/x-www-form-urlencoded; server page.
Finally, the question of garbled code. There are already two places where there will be mistakes.
1: for example, if the parameters are not encoded and passed directly, the transfer will be unsuccessful.
2: the Content-type is not set when using the POST method, and the server page cannot get the parameters sent.
3: the coding problem of sending request page and request page. In standard browsers, please make sure that the requested page and the requested page are encoded in utf-8, otherwise Chinese will be very tragic.
The copy code is as follows:
The situation of garbled codes here is very messy and difficult to rule out. So keep the two pages encoded as UTF-8 parameters after passing. It can effectively prevent garbled code
Thank you for reading this article carefully. I hope the article "sample Analysis of some garbled problems in ajax" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.