Before we get on the right track, let me first introduce JavaScript special characters to you
You can use backslashes in JavaScript to add special characters to text strings.
Insert special characters
Backslashes are used to insert ellipses, newlines, quotes, and other special characters in text strings.
Please see the JavaScript code below:
var txt="We are the so-called "Vikings" from the north." document.write(txt)
In JavaScript, strings use single or double quotes to start or end. This means that the above string will be truncated to: We are the so-called.
To solve this problem, you must precede the quotes in "Viking" with a backslash (). This converts each double quote into a literal string.
var txt="We are the so-called \"Vikings\" from the north." document.write(txt)
The JavaScript now outputs the correct text string: We are the so-called "Vikings" from the north.
Here’s another example:
document.write ("You \& me are singing!")
The above example produces the following output:
You & me are singing!
The following table lists the remaining special characters that can be added to text strings using backslashes:
Code | Output |
---|---|
' | Single quotes |
" | Double quotes |
& | Ampersand |
\ | Backslash |
n | Line break |
r | Carriage return character |
t | Tab |
b | Backspace character |
f | Page feed |
Background:
When I was doing a task today, when I used jquery's ajax to pass a long string of characters, the verification in the background was always unsuccessful. I was confused (the string was randomly generated, a specialty). After checking it all morning, It turns out that there is a number in the string I generated, and when it is passed by js, it will be understood as a connection character, and the number will be automatically changed to a space in the background, so the string generated in the background is different from the string generated in the front desk. .
Reason:
Special characters are automatically parsed after js. For example, am is a connector, parsed as a space, & is a variable connector, and when the server accepts data, the data will not be displayed after &, etc.
Solution:
1. Put the characters into the form, and then use js to submit the form to the server.
2. Replace the special characters in the characters with hexadecimal characters. The correspondence between some special characters and hexadecimal characters:
Space | / | ? | % | & | = | # | |
+ | / | ? | % | & | &3d | # |
str = str.replace(/\+/g,%2b); 将+号替换为十六进制
3、最简单地一种,使用encodeuricomponent()函数.
该方法不会对 ascii 字母和数字进行编码,也不会对这些 ascii 标点符号进行编码: - _ . ! ~ * ' ( ) .
其他字符(比如 :;/?:@&=+$,# 这些用于分隔 uri 组件地标点符号),都是由一个或多个十六进制地转义序列替换地.
jQuery ajax特殊字符参数
在做ajax登录时候遇到的一个问题,当传入参数含有特殊字符,比如:“$'#@”等。参数传递会有问题,无法正确获取。
$.ajax({ url: '/user/login.ydd', type:'post', data:'name=abce&password=abcd&pwd', success: function(data){ } })
我要传入的是用户名为:abc,密码为abcd&pwd的用户登录。但传入后台获取参数,会被password=abcd当作一个参数传递,&这个特殊将pwd分开了作为另一个参数解析了。
解决方法,这时候就需要ajax另外一种传递参数的方式
$.ajax({ url: '/user/login.ydd', type:'post', data:{'name':'abce','password':'abcd&pwd'}, success: function(data){ } })