JavaScript UTF8 page Submission data garbled how to do

Source: Internet
Author: User
Tags character set

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 whether the current page encoding and the default encoding is consistent, if inconsistent then refresh, if (Isie &&

The code is as follows Copy Code
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′"


The correct URL encoding in JavaScript
Having looked at the previous example, have you ever wondered why jquery can solve a problem that cannot be solved by escape?

For this question, I'd like to take a look at the instructions on escape in MSDN (screenshot):

MSDN is clear, and I don't need to explain.

However, I think one might ask: do I submit data by post? That's not a URL.
Yes, when the post data, the parameters are not placed in the URL, but the URL encoding is still used.
Post data is also URL-coded because the form can be submitted in get mode, and the data is submitted to the server via a URL.
Therefore, the submitted data must be URL-coded.

Let's take a look at how $.ajax handles the data submission process:

The code is as follows Copy Code

Ajax:function (origsettings) {
var s = Jquery.extend (true, {}, jquery.ajaxsettings, origsettings);

// ............... Get rid of some irrelevant code

Convert data if not already a string
if (s.data && s.processdata && typeof s.data!== "string") {
Note that the following call
S.data = Jquery.param (S.data, s.traditional);
}

Then look at the Jquery.param implementation process:

  code is as follows copy code

//Serialize An array of form elements or a set of
//Key/values into a query string
param:function (A, Tradit ional) {
    var s = [];
   //...... Get rid of some extraneous code
   
   //If A array was passed in, assume that it's an array of form Elements.
    if (Jquery.isarray (a) | | a.jquery) {
       //Serializ E The form elements
        Jquery.each (A, function () {
   & nbsp;        Add (this.name, this.value);
       });
       
   } else {
        //......... Remove some of the less-focused code
   }

Return the resulting serialization
Return S.join ("&"). Replace (R20, "+");

function Add (key, value) {
If value is a function, invoke it and return its value
Value = jquery.isfunction (value)? Value (): value;
s[S.length] = encodeURIComponent (key) + "=" + encodeuricomponent (value);
}
}

The core of this code is the implementation of the Add function, which calls the encodeURIComponent () function internally.
We should pay attention to the way jquery handles data: encodeURIComponent (key) + "=" + encodeuricomponent (value);
jquery at the end of the%20 also replaced with the + number.

In the field of web development, I think there should be no doubt about the authority of jquery, right? So I think the jquery approach is definitely right.

As can be seen from the way jquery is implemented: encodeURI () is actually not recommended for encoding URL data.

Speaking of which, I would like to say why the use of encodeURI is not recommended.
encodeURI is used to encode the entire URL string if a parameter value itself contains some special characters.
For example: key = "X?x/x&x", value = "aa=2&bb=3&cc= Chinese character." ", the result of this function will be incorrect.
It is often used to encode URL paths that contain similar Chinese characters, and are not suitable for handling URL parameters.
However, the directory name and filename in the URL path, we can choose English characters, so encodeURI usually have no chance to use.


Ajax garbled can refer to http://www.111cn.net/wy/js-ajax/44383.htm

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.