In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the solution of Chinese garbled code in AJAX". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "AJAX Chinese garbled solution" it!
AJAX Chinese questions are divided into two categories:
1) the parameters in the sending path are in Chinese, and the received parameter values in the server segment are garbled.
For example:
Var url= "a.jsproomname = Xiao Li"
XmlHTTP.open ("post", url,true)
Solution:
Use the escape () or encodeURI () method provided by javascript
For example:
Client:
Var url= "a.jsproomname = Xiao Li"
Url=encodeURI (url)
Url=encodeURI (url); / / twice, very critical [I don't know exactly why]
/ * /
Others wrote it as var url= "a.jsp?name=escape".
The function is similar to the encodeURI method.
/ * /
XmlHTTP.setrequestheader ("cache-control", "no-cache")
XmlHTTP.setrequestheader ("Content-Type", "application/x-www-form-urlencoded")
XmlHTTP.setrequestheader ("contentType", "text/html;charset=uft-8") / / specifies the encoding format of the transmitted data
XmlHTTP.open ("post", url,true)
Server side:
String name = request.getParameter ("name")
Name = java.net.URLDecoder.decode (name, "UTF-8")
2) the returned value of responseText or responseXML contains garbled codes in Chinese
Reason: when AJAX receives the value of responseText or responseXML, it is decoded according to the format of UTF-8. If the data sent by the server segment is not in the format of UTF-8, then the value of receiving responseText or responseXML may be garbled.
Solution: specify the format of the data to be sent on the server:
On the server side:
The copy code is as follows:
Response.setCharacterEncoding ("UTF-8")
Response.setContentType ("text/text;charset=UTF-8"); / / the txt text file is returned
Response.setContentType ("text/xml;charset=UTF-8"); / / returned xml file
Response.setContentType ("application/json"); / / returns the json file
PrintWriter bw = response.getWriter (); / / output to use this class
Bw.write (result)
Bw.flush ()
Bw.close ()
Summary: 1) the format of ajax submitted data defaults to utf-8, using the escape () or encodeURI () method provided by javascript. When receiving on the server side, use the java.net.URLDecoder.decode ("", "UTF-8") method to decode it.
2) the default character encoding for the data returned by xtmlhttp is utf-8, so when the server sends data to the client, it should also use utf-8 encoding.
If the above methods still can't solve the garbled problem, try saving the jsp,htm,java file in UTF-8 encoding format.
In short: the front and background data interaction using utf-8 coding on the line.
At this point, I believe that everyone on the "AJAX Chinese garbled solution" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.