A pit containing special characters in an Ajax pass.

Source: Internet
Author: User

problem Scenario : Today in testing their own hand page function, found a small bug, in the background with Ajax to send data, as long as there are some special characters in the parameters, the console will error HTTP 400 problem, in fact, the special character server can not parse. OK, the problem is found, but how to fix it!

The first thought is that the special characters are encoded in the pass, but their own write detection of special character function code found that this to detect the number of special characters are numerous, completely not come over.
Search the Internet, found that there is a ready-made method: encodeURIComponent (); Put it on the code and run, the task is solved! Ha ha. The problem is solved, but let's look at the deeper principles!

Special symbols are passed in from the foreground if there is no transcoding processing: My param is 7% and 6%7, the exception display is [my para is 7is%207%%20and%206%7], so there is an error,

Here are a list of some common characters

The hexadecimal representation of the URL special symbol in the URL that actually means the transcoding

Symbol Meaning Coding
+ The + sign indicates a space %2b
Space Use the + number or code %20
/ Separating directories and subdirectories %2f
? Separating the actual URLs and parameters %3f
% Specify special characters %25
# Represents a bookmark %23
& Delimiter between parameters %26
= The value of the specified parameter in the URL %3d


The solution is in fact similar, this to look at their own habits.

Scenario One: Var str= para.replace (/%/g, "%25");//g represents global match substitution
Scenario Two: encodeURIComponent (str); Automatic transcoding using the change method.

Storage question scheme: The internet has said that the parameters in JSON format can be solved, but itself is the use of JSON format, but there are such problems! So the question!

Transferred from: http://www.cnblogs.com/smalldark/p/6496675.html

A pit containing special characters in an Ajax pass.

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.