jquery Ajax Get garbled solution

Source: Internet
Author: User
Tags json

var data = {
Username:escape (your user name here),
Password:escape (your password),
};

var jsonstr = json.stringify (data); The Json2 method.

$.ajax ({
URL: '.. /service.asmx/login ',
Data: ' userinfo= ' + jsonstr,
ContentType: "Application/json; Charset=utf-8 ",
DataType: "Jsonp",
Type: "Get",
Success:function (response) {
...
},
Error:function (A, B, c) {
...
}
});

The

scenario is to encode the Chinese character by using the escape method of the Web effect, and then go to WebService to decode it automatically.
There is another problem today: using jquery ajax get to send Unicode characters such as Swedish characters is garbled, even with escape.
Think: A request sent through a GET method actually transmits the parameter through a URI, so the character transferred by get is independent of the encoding format of the file, and it is entirely up to the URI to decide what to transmit, so what if the URI is encoded?
In fact, JavaScript already has such a global function to encode the URI: encodeURI (URI), so that jquery Ajax sent a data encoded by the URI will not appear garbled, And the data can be decode automatically at the WebService end.
The improved code is as follows:

var data = {
     username:encodeuri (your user name here),
  &nbs p;  Password:encodeuri (your password here),
};

var jsonstr = json.stringify (data); //The Json2 method.

$.ajax ({
     URL: ' ... /service.asmx/login ',
     data: ' userinfo= ' + jsonstr,
     ContentType: "Application/json; Charset=utf-8 ",
     datatype:" Jsonp ",
     Type:" Get ",
 & nbsp;   success:function (response) {
          ...
    },
     error:function (A, B, c) {
    & nbsp;     ...
    }
});

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: [email protected] 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.