Methods for passing special characters (+, &) in js _ javascript tips-js tutorial

Source: Internet
Author: User
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.

Related Article

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.