【问题标题】:Which EncodeFor should be used for location?哪个 EncodeFor 应该用于定位?
【发布时间】:2018-10-09 21:00:47
【问题描述】:

应该使用哪个EncodeForlocation()

如果我想通过位置推送一些数据,它应该是什么样的?

location("obtainBDK.cfm?message=#ErrorMessage#", false); // nothing

location("obtainBDK.cfm?message=#EncodeForHTMLAttribute(ErrorMessage)#", false);

location("obtainBDK.cfm?message=#EncodeForURL(ErrorMessage)#", false);

还有什么?

【问题讨论】:

    标签: coldfusion cfml coldfusion-2018


    【解决方案1】:

    cflocation/location 设置Location HTTP 标头。浏览器读取这个值并通过 HTTP GET 请求提到的资源。所说的 URI 应该被编码。

    现在唯一需要编码的 URI 部分是查询字符串,它以问号 ? 开头。每个键值对由编码键、等号= 和编码值组成。多对由 & 符号 & 分隔。

    根据RFC 1738

    因此,只有字母数字、特殊字符“$-_.+!*'()”以及用于其保留目的的保留字符可以在 URL 中未编码使用。

    保留字符示例

    未编码的 URI:
    http://example.org/path?&=&&===&?

    预期的键值对:

    - "&": "&"
    - "=": "="
    - "?": ""
    

    但是,正确的解析器只会看到空的键和值。我们需要对键和值进行编码,以免出于技术目的而对其进行处理。

    编码的 URI: http://example.org/path?%26=%26&%3D=%3D&%3F&%20=%20!

    现在 key 和 value 中的所有字符都按照RFC 3986 进行百分比编码,不会被解析器弄错。

    ColdFusion:

    kvps = [];
    
    key = "message";
    val = ErrorMessage;
    kvps.append(
        urlEncodedFormat(key) & "=" & urlEncodedFormat(val)
    );
    
    targetUrl = "btainBDK.cfm?" & arrayToList(kvps, "&");
    location(targetUrl, false);
    

    urlEncodedFormat 与 encodeForUrl

    虽然……

    Adobe recommends that you use the EncodeForURL function, not the URLEncodedFormat function, to escape special characters in a string for use in a URL in all new applications.

    我遇到了无法正确区分+ 是空格还是实际加号的问题,尤其是在上下文更改时(CF JS)。所以我会推荐urlEncodedFormat,不管Adobe对此有何看法。

    【讨论】:

      猜你喜欢
      • 2019-03-17
      • 2020-01-20
      • 1970-01-01
      • 2019-08-30
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      相关资源
      最近更新 更多