使用“&"和"+"在查询字符串中 [英] Using "&" and "+" in Querystring

查看:49
本文介绍了使用“&"和"+"在查询字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Javascript调用以下URL.

In call the following URL in Javascript.

var par = "Participant.aspx?ID=" + Id + "&NAME=" + Name+ "&FIRSTNAME=" + Firstname;

有时,名称"或名字"包含一个&"号或一个加号".

Sometimes the Name or the Firstname contains an "ampersand" or an "plus" sign.

For Example:        Richard & Michael or Richard + Michael

在服务器端,我这样读取查询字符串:

On the server side, I read the query string like this:

        Dim Name As String = Request.QueryString("NAME")
        Dim Firstname As String = Request.QueryString("FIRSTNAME")

我的问题是,如果查询字符串包含加号",那么该符号将转换为空格(''),如果查询字符串包含与号",则删除与"号之后的所有内容.

My problem is that if the query string contains a "plus" sign, then the sign is converted to space ( ' ' ) and if the query string contains a "ampersand", then everything after the "ampersand" is deleted.

我尝试了几种类似的方法.Request.Form而不是Reqeust.Querystring,我也尝试了Server.URLEncode.但是两者都不适合.

I have tried several things like. Request.Form instead of Reqeust.Querystring and i also tried Server.URLEncode. But both not working suitable.

使用URLEncode是一个问题,如果查询字符串在名称"和加号"之间包含空格,则空格也将转换为加号".

With URLEncode is the problem that if the Querystring contains spaces between the Name and the "plus" sign, the spaces also get converted to "plus" signs.

您有解决方法的想法吗?

Do you have an idea how to solve this?

推荐答案

请记住,查询字符串的内容(名称和值)必须正确URI编码.如果该行在JavaScript中,则可以这样操作:

Remember that the contents of the query string (both the names and values) must be correctly URI-encoded. If that line is in JavaScript, you'd do that like this:

var par = "Participant.aspx?ID=" + encodeURIComponent(Id) +
            "&NAME=" + encodeURIComponent(Name) +
            "&FIRSTNAME=" + encodeURIComponent(Firstname);

(从技术上讲,同样,名称也应进行编码,但"ID","NAME"和"FIRSTNAME"的编码方式完全相同,因此我没有在 encodeURIComponent 上使用他们.)

(Technically, again, the names should be encoded too, but "ID", "NAME", and "FIRSTNAME" encode to exactly the same thing, so I didn't use encodeURIComponent on them.)

请参阅 AntP的评论有关加号和空格:

See AntP's comment re the plus signs and spaces:

使用URLEncode的问题是,如果查询字符串在名称"和加号"之间包含空格,那么空格也将转换为加号"." -这就是应该发生.加号表示空格.编码的加号表示加号.

"With URLEncode is the problem that if the Querystring contains spaces between the Name and the "plus" sign, the spaces also get converted to "plus" signs." - that is what should happen. A plus sign denotes a space. An encoded plus sign denotes a plus sign.

这篇关于使用“&"和"+"在查询字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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