This article describes how to pass special characters (+, & amp;) in Javascript. If you need it, refer to the background:
When I was doing a task today, when I passed a long string of characters using Jquery Ajax, the verification in the background was always unsuccessful. When I struggled (the string was randomly generated and specialized ). After checking for the morning, the original string generated by me contains the plus sign (+), which is understood as a connection character during js transmission, at the backend, The + sign is automatically changed to a space, so the background string is different from the one generated at the front end.
Cause:
Special characters are automatically parsed after js, such as the connector +, space, and variable connector. When the Server accepts data, the data in the future is not displayed.
Solution:
1. Place the characters in form and submit the form to the server using js.
2. Replace special characters with hexadecimal characters. The relationship between some special characters and hexadecimal is as follows:
+ |
Space |
/ |
? |
% |
& |
= |
# |
% 2B |
% 20 |
% 2F |
% 3F |
% 25 |
% 26 |
& 3D |
% 23 |
Str = str. replace (/\ +/g, "% 2B"); replace the "+" with the hexadecimal notation.
3. The simplest method is to use the encodeURIComponent () function.
This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().
Other characters (such :;/? : @ & =+ $, # The punctuation marks used to separate URI components) are all replaced by one or more hexadecimal escape sequences.