background:
When doing a task today, when using jQuery's ajax to pass a long string of characters, the verification of the background has been unsuccessful. When I was tangled, I was randomly generated. After checking the morning, it turned out that there was+number in the string I generated, and when the JS passed, I would understand it as a connection character. In the background It is different from the front desk.
reason:
After JS, automatically analyze the special characters, such as+number is the connector, parsing as a space, & is a variable connector, the data on the server does not display the data after receiving data, and so on.
Solution:
1. Put the characters in Form, and then submit the FORM form to the server with JS.
2. Replace the special characters in the characters to the hexadecimal character. Some special characters and the corresponding relationship between hexadecimal:
| + | Space | / / | What's more? | % | & | = | # # |
| %2B | %20 | %2F | %3F | %25 | %26 | & 3D | %twenty three |
str = str.replace (//+/g, "%2B"); replace+number to hexadecimal production
3. The simplest one is to use the enCodeuricomponent () function.
This method will not encode ASCII letters and numbers, nor will they code these ASCII punctuation symbols: -_.! ~ * '().
Other characters (such as:;/?:@& =+$, #The punctuation symbols for separating URI components) are replaced by one or more hexadecimal transition sequences.