ajax - 为什么jquery替换“+”有空格(“”)? [英] ajax - Why jquery replaces "+" with a space (" ")?

查看:163
本文介绍了ajax - 为什么jquery替换“+”有空格(“”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到问题。当我使用ajax将包含+的参数传递给我的控制器时,它被替换为空格。

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.

示例,我将传递value = Tom + Jerry + Garfield 使用ajax。当我在控制器中使用System.out.println()时,它会显示 Tom Jerry Garfield 。我尝试使用其他特殊字符我似乎没有问题。

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.

请帮忙。在此先感谢。

推荐答案

在某些 GET POST 请求(很可能在URL中或通过表单),空格在传递到服务器之前被编码为+(加号)符号。如果您执行正常的 GET 请求,您会看到此行为 - 您会看到类似 google.com?q=test+example 如果你想通过ajax GET / POST请求传递加号,你需要urlencode它。 + 的URL编码值为%2B

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.

另请注意:

可以使用javascript encodeURIComponent()函数,如下所示:

The javascript encodeURIComponent() function can be used, as answered in:

AJAX POST和加号(+) - 如何编码?

这篇关于ajax - 为什么jquery替换“+”有空格(“”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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