Background:
Today in doing a task, using jquery Ajax to pass a long string of characters, in the background of the validation has been unsuccessful, tangled with me (that string is randomly generated, expertise). Check the morning, the original is I generated the string has + number, and in the time of JS delivery, will be understood to be connected characters, to the background will be the + automatically into space, so the background of the string and the foreground generated is not the same.
Reason:
JS after the automatic resolution of special characters, such as the + number for the connector, resolved to the space,& as a variable connector, server-side acceptance of data & after the data does not show and so on.
Solution:
1, put the characters in the form, and then use JS to submit form form to the server.
2. Replaces special characters in characters with 16-character characters, and some special characters correspond to hexadecimal:
+ |
Space |
/ |
? |
% |
& |
= |
# |
%2b |
%20 |
%2f |
%3f |
%25 |
%26 |
&3d |
%23 |
str = str.replace (/\+/g, "%2b"); Replace the + number with hexadecimal
3, the simplest one, using the encodeURIComponent () function.
The method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks:-_. ! ~ * ' ().
Other characters (such as:;/?:@&=+$,# the punctuation marks used to separate the URI components) are replaced by one or more hexadecimal escape sequences.