The method of passing special character (+,&) in JS _javascript skill

Source: Internet
Author: User

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.

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.