java ajax - 为什么 jquery 用空格(“”)替换“+”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11447391/
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
ajax - Why jquery replaces "+" with a space (" ")?
提问by NinjaBoy
I am having a problem here. When I use ajax to pass a parameter containing "+" to my controller it is being replaced by a space.
我这里有问题。当我使用 ajax 将包含“+”的参数传递给我的控制器时,它被一个空格替换。
Example, I will pass value = Tom+Jerry+Garfield
using ajax. When I use System.out.println() in my controller it displays Tom Jerry Garfield
. I tried using other special characters I don't seem to have a problem.
例如,我将Tom+Jerry+Garfield
使用 ajax传递 value = 。当我在控制器中使用 System.out.println() 时,它显示Tom Jerry Garfield
. 我尝试使用其他特殊字符我似乎没有问题。
Please help. Thanks in advance.
请帮忙。提前致谢。
回答by compid
In some GET
and POST
requests (most likely in the URL, or via a form), spaces are encoded as "+" (plus) symbols before they are passed to the server. You can see this behaviour if you do a normal GET
request - you will see something like google.com?q=test+example
If you want to pass a plus symbol via an ajax GET/POST request, you need to "urlencode" it. The URL encoded value for +
is %2B
.
在某些GET
和POST
请求中(最有可能在 URL 中,或通过表单),空格在传递到服务器之前被编码为“+”(加号)符号。如果您执行普通GET
请求,您可以看到这种行为- 您会看到类似的内容google.com?q=test+example
如果您想通过 ajax GET/POST 请求传递加号,您需要对其进行“urlencode”。对于URL编码值+
是%2B
。
Also note:
另请注意:
The javascript encodeURIComponent() function can be used, as answered in:
可以使用 javascript encodeURIComponent() 函数,如下所述:
回答by xdazz
+
is decoded as space after url decoding. If you want to pass +
, you need to encode it.
+
url解码后被解码为空格。如果要通过+
,则需要对其进行编码。
回答by bhuvin
When we pass values to the controller there is a model binder which is sitting in between the request. When the ajax call is made the url and the request is encoded. The " " (Space) character in url decoded form encodes to a "+".
The Model Binder on the other hand decodes the request and extracts the parameters and gives it to the controller and hence "+" is converted to a " " .
But here the question is why would one pass "+" as a separator ??
当我们将值传递给控制器时,有一个模型绑定器位于请求之间。当进行 ajax 调用时,url 和请求被编码。url 解码形式的“”(空格)字符编码为“+”。
另一方面,模型绑定器解码请求并提取参数并将其提供给控制器,因此“+”被转换为“”。但这里的问题是为什么要通过“+”作为分隔符?