Home  >  Article  >  Web Front-end  >  Two methods to solve javascript url post special character escape + & #_jquery

Two methods to solve javascript url post special character escape + & #_jquery

WBOY
WBOYOriginal
2016-05-16 15:05:443121browse

Recently, I found that data was lost when using URL to transmit special characters.

In fact, it is not lost. For example, ‘+’ will be treated as a connecting character.

1. Replace with hexadecimal characters. Here are some common conversions

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

Copy code The code is as follows:
var post_Str = apply_name.replace(/+/g, "%2B ");//"+"escape
var post_Str= post_Str.replace(/&/g, "%26");//"&"
var post_Str= post_Str.replace(/#/g, "%23");//"#"

2. If you encounter other special characters, you can use the following method to see what to convert it into. Just write one according to the above method

Copy code The code is as follows:
alert(encodeURIComponent("-"));//Transcoding, It’s OK to get the result. Below is the decoding, which is not used here
alert(decodeURIComponent(encodeURIComponent("#")));
Statement:
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