Html 如何在查询字符串中包含特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2828211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:04:57  来源:igfitidea点击:

How to include special characters in query strings

htmlurl-rewriting

提问by KentZhou

The following URL works fine:

以下网址工作正常:

http://localhost/mysite/mypage?param=123

http://localhost/mysite/mypage?param=123

However, if I want to put some special characters in `param like ?, /, \, then the URL becomes:

但是,如果我想在 `param 中添加一些特殊字符,如 ?、/、\,则 URL 变为:

http://localhost/mysite/mypage?param=a=?&b=/

http://localhost/mysite/mypage?param=a=?&b=/

or

或者

http://localhost/mysite/mypage?param=http://www.mysite.com/page2?a=\&b=...

http://localhost/mysite/mypage?param=http://www.mysite.com/page2?a=\&b=...

which won't work. How do I resolve this issue?

这是行不通的。我该如何解决这个问题?

采纳答案by Steven P.

You have to encode special characters in URLs. See: http://www.w3schools.com/tags/ref_urlencode.asp

您必须对 URL 中的特殊字符进行编码。见:http: //www.w3schools.com/tags/ref_urlencode.asp

回答by Kop4lyf

You need to encode the query parameters before combining them to form a url. The function needed here is encodeURIComponent.For example,

您需要先对查询参数进行编码,然后再将它们组合成一个 url。这里需要的函数是encodeURIComponent。例如,

the url you need to create is:

您需要创建的网址是:

http://localhost/mysite/mypage?param=a=?&b=/

Now, assuming that ? and / comes as variables, you need to encode them before putting in the url. So lets create your url using this function(I am expecting two query parameters):

现在,假设 ? 和 / 作为变量,您需要在放入 url 之前对它们进行编码。所以让我们使用这个函数创建你的 url(我期待两个查询参数):

 var q1 = "a=?"; //came from some input or something
    var q2 = "/"; //came from somewhere else

    var faultyUrl =  "http://localhost/mysite/mypage?param="+ q1 +"&b=" + q2; 
// "http://localhost/mysite/mypage?param=a=?&b=/"


    var properUrl =  "http://localhost/mysite/mypage?param="+ encodeURIComponent(q1) +"&b=" + encodeURIComponent(q2); 
//"http://localhost/mysite/mypage?param=a%3D%3F&b=%2F"

This function is in basic JS and supported in all the browsers.

这个函数是基本的JS,所有浏览器都支持。

回答by Vishal Seth

In JavaScript you can use the encodeURI()function.

在 JavaScript 中,您可以使用encodeURI()函数。

ASP has the Server.URLEncode()function.

ASP 具有Server.URLEncode()函数。

HttpServerUtility.UrlEncodein .NET

.NET 中的HttpServerUtility.UrlEncode

回答by Hardik Mandankaa

Easy way to pass QueryString value with special character using javascript:

使用 javascript 传递带有特殊字符的 QueryString 值的简单方法:

var newURL=encodeURIComponent(uri);
window.location="/abc/abc?q="+newURL;

回答by JYelton

You need to use encode special characters, see thispage for a reference.

您需要使用编码特殊字符,请参阅页面以获取参考。

If you're using PHP, there's a function to do this, called urlencode().

如果您使用 PHP,则有一个函数可以执行此操作,称为urlencode()

回答by Moonshield

You need to substitute the characters with URL entities. Some information here.

您需要用 URL 实体替换这些字符。 这里有一些信息。

回答by Thavaprakash Swaminathan

I did below, it works fine.

我在下面做了,它工作正常。

const myQueryParamValue = "You&Me";
const ajaxUrl = "www.example.com/api?searchText="+encodeURIComponent(myQueryParamValue)