Home > Web Front-end > JS Tutorial > Method of passing special characters (,&) in js_javascript skills

Method of passing special characters (,&) in js_javascript skills

WBOY
Release: 2016-05-16 17:03:40
Original
1167 people have browsed it

背景:

今天在做一个任务时,用Jquery的Ajax传递一长串字符时,在后台的验证一直不成功,纠结时我了(那个字符串是随机生成的,特长)。查了一上午,原来是我生成的字符串中有+号,而在js传递的时候,会理解为是连接字符用的,到了后台就将+号自动变为空格了,所以后台的字符串和前台生成的已经不一样了。

原因:

js后自动解析特殊字符,如+号为连接符,解析为空格,&为变量连接符,服务器端接受数据时&以后的数据不显示等等。

解决办法:

1、将字符放到form中,然后用js提交form表单到服务器。

2、将字符中的特殊字符替换成十六进制的字符,一些特殊字符与十六进制的对应关系:

 

+ 空格 / ? % & = #
+ / ? % & &3D #

str = str.replace(/ /g,"+"); Replace number with hexadecimal

3. The simplest one is to use the encodeURIComponent() function. ​

 This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation characters: - _ . ! ~ * ' ( ) .

Other characters (such as :;/?:@&= $,# which are punctuation marks used to separate URI components) are replaced by one or more hexadecimal escape sequences.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template