The problem of URL path and parameter special characters in Ajax

Source: Internet
Author: User


Recently in the project, the Ajax parameters appear in the special characters (#) cause the path truncation problem, go to Niang find the answer, found the solution, in JS there is a: Escape () function
Look at the function introduction
The escape () function encodes the string so that it can be read on all computers.
Example
<script type= "Text/javascript" >
document.write (Escape ("Visit w3school!") + "<br/>")
document.write (Escape ("?! = () #%& "))
</script>

Output:
Visit%20w3school%21
%3f%21%3d%28%29%23%25%26


If there is a conversion of this URL encoding, then we can decode it in the background via Urldecoder.decode (string s, String enc) and convert it back.


Although the problem solved, but because of curiosity in-depth look, found a problem is JS in Escape () there is a flaw, please note:
Description: The method does not encode ASCII letters and numbers, nor does it encode the following ASCII punctuation marks: * @-_ +. / 。 All other characters will be replaced by escape sequences.
Find out, the Escape () method is not URL-encoded for all special characters. So what if we have these things in demand?
Workaround 1. Replace the characters manually by using the Replace method. (attached, common URL code)

List of URL encodings for common characters:

URL encoding of reserved characters
! * " ( ) ; : @ &
%21 %2A %22 %27 %28 %29 %3B %3A %40 %26
= + / % #
%3D %2B %24 %2C %2F %3F %25 %23 %5B

%5D




Workaround 2. Use a different% encoding function. The following are the relevant methods in JS


Escape () Method:

Encodes the specified string using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters are converted to the character encoding in the%xx format (XX equals the encoded 16-digit number of the character in the character set table). For example, the encoding for a space character is%20.

Characters that are not encoded by this method: @ */+

encodeURI () Method:

Converts the URI string into an escape format using the UTF-8 encoding format.

Characters that will not be encoded by this method:! @ # $& * () =:/;? +

encodeURIComponent () Method:

Converts the URI string into an escape format using the UTF-8 encoding format. Compared to encodeURI (), this method encodes more characters, such as characters. So if the string contains several parts of the URI, it cannot be encoded in this way, otherwise the URL will display an error after the/character is encoded.


Characters that will not be encoded by this method:! * ( ) ‘

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The problem of URL path and parameter special characters in Ajax

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.