Ajax garbled Problem Solution

Source: Internet
Author: User
During WEB development, consistent data encoding is often required at the front and back ends!

On pages with non-English character sets, if Ajax is used for data interaction, you must ensure the unified encoding of the front and back ends of the data. Otherwise, garbled characters may easily occur!

When the backend is an ASP program, you can use the following functions to ensure the unified encoding of the values transmitted between the front-end Javascript and Asp:

Encoding: escape (string)

Decoding: unescape (string)

Both functions exist in JavaScript and Asp, and the functions are the same. As long as any end transmits data to the other end, escape is used for encoding, the recipient end is decoded using unescape to ensure that no garbled code occurs during Javascript and Asp data transmission!

When the backend is a PHP program, you can use the following functions to ensure the unified encoding of the values transmitted between the front-end Javascript and PHP:

WEB Front-end JavaScript

Encoding: encodeURI (string)

Decoding: decodeURI (string)

WEB backend Php

Encoding: urlencode (string)

Decoding: urldecode (string)

Similarly, when passing values, use the corresponding encoding function encodeURI or urlencode. When receiving values, use the corresponding decoding function decodeURI or urldecode to ensure that no garbled code occurs during Javascript and Php data transmission!

In the WEB Front-end Javascript decoding, when urlencode-encoded data in Php, it may need to be decoded twice, for example:

Unescape (decodeURI (string ))

Unescape is used to extract non-character texts, such as punctuation marks.

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.