jQuery ajax传递特殊字符串问题

WBOY
Release: 2016-06-01 09:54:14
Original
1115 people have browsed it

当你用ajax传递值到服务器端,如果值中包含特殊字符串如+,&等,在服务器端获取的结果可能就会出现差异,因为这些字符有其它用途,如“+”表示连接符,在转义后你获取到的就是空格。
可以看看这些特殊字符与十六进制的关系:

+ 空格 / ? % & = #
%2B %20 %2F %3F %25 %26 &3D %23

如果变量中确实需要包含这些特殊字符,请用encodeURIComponent()函数过滤,它会对这些字符编码,服务器端是会被自动解码的,不需要处理。
另外,尽量提交json格式数据 如 {a:"aaa", b:"bbb"} 而非 "a=aaa&b=bbb"

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!