Solution to ajax request garbled characters

Source: Internet
Author: User

This article describes how to solve ajax request Garbled text (Chinese Garbled text). For more information, see

Today, I encountered a problem with garbled characters concerning the transmission of Chinese Characters in ajax requests.

 

The following code:

The Code is as follows:

Function UpdateFolderInfoByCustId (folderId, folderName, custId ){

$. Ajax ({

Type: "Post ",

ContentType: "application/x-www-form-urlencoded; charset = UTF-8 ",

Url: "http: // localhost/CRM/Ashx/HandKBSucessCustomer. ashx? Method = UpdateCustomerByCustId & folderId ="

+ FolderId + "& folderName =" + encodeURI (folderName) + "& custId =" + custId,

Success: function (msg ){

Alert (msg );

},

Error: function (error ){

Alert (error );

}

});

}

 

 

If the above Code only transmits "& foderName =" + folderName, Chinese characters will produce garbled characters. If it is converted twice through encodeURL, the Chinese character encoding will become similar

 

"% E6 % b5 % 8b % eb % af % 95" format. After being converted to this format, transcoding is performed when obtaining it, as shown below:

The Code is as follows:

Public void UpdateCustomerByCustId ()

{

Int folderId = Convert. ToInt32 (Request ["folderId"]);

String folderName = Request ["folderName"];

String folderName2 = Convert. ToString (System. Web. HttpUtility. UrlDecode (folderName ));

Int custId = Convert. ToInt32 (Request ["custId"]);

Bool res = false;

Try

{

Res = CustomerBusiness. UpdateCustomerByCustId (folderId, folderName2, custId );

}

Catch (Exception ex)

{

Throw;

}

Response. Write (res );

}

}

}

 

 

After conversion, you can obtain the transmitted Chinese characters.

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.