应该使用哪个 EncodeFor 进行定位?

Which EncodeFor should be used for location?

应该使用哪个 EncodeFor location()?

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

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

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

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

还有别的吗?

cflocation/location 设置 Location HTTP header。浏览器读取此值并通过 HTTP GET 请求上述资源。应该对所述 URI 进行编码。

现在唯一需要编码的 URI 部分是查询字符串,它以问号 ? 开头。每个 key-value-pair 由编码密钥、equal-sign = 和编码值组成。多对由符号 &.

分隔

根据RFC 1738

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.

保留字符示例

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

预计 key-value-pair秒:

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

但是,正确的解析器只会看到空的键和值。我们需要对键和值进行编码,这样它们就不会被用于技术目的。

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

现在键和值中的所有字符根据RFC 3986都是percent-encoded并且不会被解析器弄错。

冷聚变:

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.

我遇到了无法正确区分 + 是 space 还是实际加号的问题,尤其是当上下文发生变化时 (CF <-> JS)。因此,无论 Adob​​e 对此有何看法,我都会推荐 urlEncodedFormat