php将空格转为为加号,使用Post提交时须将空格转换成加号的解释

jQuery的serialize模块中有个r20正则

复制代码 代码如下:

var r20 = /%20/g,

jQuery.param方法中会将所有的"%20"转成"+",即提交数据前,数据中如果包含空格,那经过encodeURIComponent后,空格会转成"%20"

复制代码 代码如下:

encodeURIComponent(' ') === '%20'; // true

最后需要将"%20"转换成"="再Post提交。这样后台程序接受到的才是真正的空格。

关于 encodeURIComponent,见MDC描述

encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )

To avoid unexpected requests to the server, you should call encodeURIComponent on any user-entered parameters that will be passed as part of a URI. For example, a user could type "Thyme &time=again" for a variable comment. Not using encodeURIComponent on this variable will give comment=Thyme%20&time=again. Note that the ampersand and the equal sign mark a new key and value pair. So instead of having a POST comment key equal to "Thyme &time=again", you have two POST keys, one equal to "Thyme " and another (time) equal to again.

For application/x-www-form-urlencoded (POST), per http://www.w3.org/TR/html401/interac...m-content-type, spaces are to be replaced by '+', so one may wish to follow a encodeURIComponent replacement with an additional replacement of "%20" with "+".

相关:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent

http://www.w3.org/TR/html401/interact/forms.html#form-content-type

时间: 2013-01-11

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们使用RestTemplate请求URL时,如果URL中包含加号("+"),我们需要进行URL编码,将加号替换为"%2B"。 RestTemplate是Spring Framework提供的用于进行HTTP客户端请求的工具类。在使用RestTemplate发送请求时,我们可以使用`RestTemplate`类的`getForObject()`或`postForObject()`等方法来发送GET或POST请求,并接收返回的结果。 在构造URL时,如果URL中包含特殊字符如空格加号等,为了确保URL的正确性和安全性,我们需要对这些特殊字符进行URL编码。URL编码是一种将URL中的特殊字符转换为特定字符序列的过程,以确保URL可以被正确解析和处理。 对于加号("+")来说,由于它在URL中有特殊的含义(用于表示空格),因此在拼接URL时需要将加号替换为"%2B"。 示例代码如下: ```java import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; import java.net.URLEncoder; public class RestTemplateExample { public static void main(String[] args) throws Exception { RestTemplate restTemplate = new RestTemplate(); // 创建HTTP请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); // 对URL中的加号进行编码 String url = "http://example.com/api/foo+bar"; String encodedUrl = URLEncoder.encode(url, StandardCharsets.UTF_8.toString()).replace("+", "%2B"); // 发送GET请求 ResponseEntity<String> response = restTemplate.getForEntity(encodedUrl, String.class); // 输出响应结果 System.out.println(response.getBody()); } } ``` 在上述示例代码中,我们首先创建了一个`RestTemplate`实例。然后,我们创建一个`HttpHeaders`对象,设置Content-Type为"application/json"。接下来,我们对URL中的加号进行URL编码,并将加号替换为"%2B"。最后,我们使用`RestTemplate`的`getForEntity()`方法发送GET请求,并将响应结果保存在`ResponseEntity`中,通过`getBody()`方法获取结果。 使用URL编码后的URL,可以确保URL中的加号被正确解析和处理,可以正常发送请求并获取响应结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值