用javascript编码url而不编码& [英] encoding url in javascript not encoding &

查看:102
本文介绍了用javascript编码url而不编码&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用javascript编码以下字符串

encodeURI = "?qry=M & L";

这给了我一个输出

qry=m%20&%20l

因此,&" M & L中的来自未编码.我该怎么办?

解决方案

不对具有特殊含义的字符进行编码(保留 字符).以下示例显示了所有 URI方案"可能包含.

参考

encodeURI 不会编码以下特殊字符

A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

 let uri = "?qry=M & L"

console.log(encodeURI(uri)) 

因此您可以使用 encodeURIComponent ,这将对除这些字符之外的所有字符进行编码

A-Z a-z 0-9 - _ . ! ~ * ' ( )

 let uri = "?qry=M & L"

console.log(encodeURIComponent(uri)) 

I am encoding the following string in javascript

encodeURI = "?qry=M & L";

This give me an output

qry=m%20&%20l

So the "&" from M & L is not getting encoded. What should i do?

解决方案

Does not encode characters that have special meaning (reserved characters) for a URI. The following example shows all the parts that a URI "scheme" can possibly contain.

Reference

encodeURI will not encode following special charcaters

A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

let uri = "?qry=M & L"

console.log(encodeURI(uri))

So you can use encodeURIComponent ,this will encode all the characters except these

A-Z a-z 0-9 - _ . ! ~ * ' ( )

let uri = "?qry=M & L"

console.log(encodeURIComponent(uri))

这篇关于用javascript编码url而不编码&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆