且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ajax - 为什么jquery替换“+”有空格(“”)?

更新时间:2023-02-23 10:36:55

在某些 GET POST 请求(很可能在URL中或通过表单),空格在传递到服务器之前被编码为+(加号)符号。如果您执行正常的 GET 请求,您会看到此行为 - 您会看到类似 google.com?q=test+example $ c的内容$ c>如果你想通过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和加号(+) - 如何编码?