JavaScript Ajax submission Data Chinese garbled solution

Source: Internet
Author: User
Tags character set response code

A UTF8 page requires post data to the GB2312 page, when the Chinese encoding is submitted in accordance with the UTF8, GB2312 the page can not receive. How to achieve, the network search a very good way, but there is a flaw, here one.

1, the form tag plus Accept-charset code, this can automatically encode form data into the specified character set submission, such as in the UTF8 page to submit data to GB2312, the code is accept-charset= "Gb2312″, But accept-charset except IE other browser all support ...

2, at this time can trigger js,document.charset= ' gb2312′ when submitting, use this code to set the current page encoding as GB2312. Here seems to be perfect, but also a problem, is triggered this code, the current page after the refresh will be garbled, this is because you have just changed the current page encoding.

3, how to deal with the refresh after garbled, is to be judged to determine the current page encoding and the default encoding is consistent, if inconsistent then refresh,

The code is as follows Copy Code

if (Isie && document.charset!= "Utf-8″") Location.reload (False)

4, the introduction of the Internet up to this step, but I found that this has entered a dead loop, because the 2nd step has changed the code, you are in the current page in any case refresh, or change after the code, so be sure to reset this code just line, this is the case.

The code is as follows Copy Code

if (Isie && document.charset!= "Utf-8″") {
Document.charset= ' utf-8′;
Location.reload (FALSE);

The code is combined as follows:
Determine if the current page number is UTF8, if not, set the encoding to UTF8 and refresh to avoid garbled.

The code is as follows Copy Code

var isie=!! Window. ActiveXObject;
if (Isie && document.charset!= "Utf-8″") {
Document.charset= ' utf-8′;
Location.reload (FALSE);

Set the form Accept-charset, so that non-IE browsers directly submit encoded data to other pages

The code is as follows Copy Code

<form accept-charset= "gb2312″>

Trigger to modify the current page encoding JS when submitting data

The code is as follows Copy Code

Onsubmit= "if (isie) document.charset= ' gb2312′"

Instance

The method is actually very simple:

For example, the page form (form) code is as follows:

The code is as follows Copy Code

<form name= "FormName" method= "POST" action= "" >
......
</form>

Just add JavaScript functions to the page, such as:

The code is as follows Copy Code

function functionname ()
{
Document.charset= "gb2312";
Formname.submit ();
}

Finally, add this function to the form's onsubmit. That

The code is as follows Copy Code

<form name= "FormName" method= "post" action= "onsubmit=" functionname (); " >
......
</form>

Let's look at the classic Ajax request code first.

The code is as follows Copy Code

Xmlhttp.open ("Post", url, async);
Xmlhttp.setrequestheader ("Content-type", "text/html");
Xmlhttp.send (params);

Through the previous instructions, do not know you now see the clue is not. Do not know is affected by the online tutorials or other aspects of the impact, setRequestHeader and is a constant, and no one wants to change it, and the problem is just out of this place. Recall the encoding setting for the content of a JSP page, which has this section:

The code is as follows Copy Code

Contenttype= "text/html; Charset=utf-8 "

Now that we know the problem, we have to change the second code to read:

The code is as follows Copy Code

Xmlhttp.setrequestheader ("Content-type", "text/html;charset=utf-8");

If the form is submitted, then set to

The code is as follows Copy Code
"Application/x-www-form-urlencoded; Charset=utf-8 "

Finally, don't forget to set it up when you return the data:

The code is as follows Copy Code

Response.setcontenttype ("Text/xml");
Response.setcharacterencoding ("UTF-8");

If you want to ask why, in fact, we can see XMLHTTP as a temporary page, which is dynamically generated by the browser, the main role is to obtain the requested data in the background (can be seen as a high-level iframe). So for the normal page to set the encoding, it should also set the same. And in the servlet to return the data why to set contenttype and encoding is the same reason. As we all know, the final form of JSP is the servlet, and the JSP page the first set of the content is to let the generated servlet generated so two words:

The code is as follows Copy Code

Response.setcontenttype ("text/html");
Response.setcharacterencoding ("UTF-8");

Pageencoding, however, tells the JVM what encoding to use for this page's content (which is related to the generated class later). So it's a matter of course to set the response code in the servlet.

The code is as follows Copy Code

Response.setcontenttype ("Text/xml;charset=utf-8");

Response.setheader ("Pragma", "No-cache"); HTTP 1.0

Response.setdateheader ("Expires", 0); Prevents caching at the proxy server

PrintWriter out = Response.getwriter ();

Out.write (Outxml);

Out.flush ();

Out.close ();

Ok! This to the client to write the data in Chinese is also UTF-8 encoding, client-side JS script to get to the Request.responsexml or ResponseText, the inside of the data will not have garbled

7 to solve the JSON in the background of the garbled message:

A.action:

The code is as follows Copy Code

PrintWriter out = Response.getwriter ();
Out.print ("Ah");

B.action:

The code is as follows Copy Code

BufferedReader bf = new BufferedReader (New InputStreamReader (Httppost.getresponsebodyasstream ()));

Here the bf.readline will be garbled, the solution is:

In A. Action in PrintWriter out = Response.getwriter (); Before adding

The code is as follows Copy Code

Response.setcharacterencoding ("UTF-8");

In B. Action in the Fix change to

The code is as follows Copy Code

BufferedReader bf = new BufferedReader (New InputStreamReader (Httppost.getresponsebodyasstream (), "UTF-8"));

Summarize
In fact, as long as the page and save the data page encoding statistics is the same time, the AJAX default submission data encoding for UTF-8, if you are GBK page we just send the code to modify it.

Chinese garbled reason is that JavaScript uses UTF-8 International code, UTF-8 each Chinese character with 4 bytes to store. and my page and database are encoded with GB2312, which caused the AJAX send data when the problem of Chinese garbled

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.