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.