@在传入URL时被转换成字符串

@ being converted into string when passing in the URL

我正在尝试通过 HTTP 请求将 post 数据发送到 URL,当我尝试传递电子邮件时它会将 @ 转换为 %252540.

这是URL:

window.open('https://secure.rspcdn.com/xprr/red/PID/3428/SID/rentown?email=amrinder@odz.com');

请看一下给出的fiddle:

https://jsfiddle.net/amrindernoor/b8uvwr86/

如何避免这个问题?

使用 encodeURIComponent 作为电子邮件地址参数:

window.open('https://secure.rspcdn.com/xprr/red/PID/3428/SID/rentown?email=' + encodeURIComponent('amrinder@odz.com'))

email 参数(由 encodeURIComponent 返回)的正确值是 ˙amrinder%40odz.com˙,它将被解释为 amrinder@odz.com 服务器端。

更改后,URL returns 302 重定向到 https://www.rsptrack.com/click.track?CID=287283&AFID=276422&SID=rentown&SID2=n&SID3=n&email=amrinder%40odz.com&zid=f197f1cfb16ae7d56748bca35ebe7658&tkp=3428&tku=4160&tks=86073803,似乎包含正确的电子邮件参数值。

您可以使用encodeURIComponent()函数来完成它。此函数对特殊字符进行编码。此外,它对以下字符进行编码:, / ? : @ & = + $ #

window.open('https://secure.rspcdn.com/xprr/red/PID/3428/SID/rentown?email=' + encodeURIComponent('amrinder@odz.com'))

注:

Use the decodeURIComponent() function to decode an encoded URI component.

您的 URL https://secure.rspcdn.com/xprr/red/PID/3428/SID/rentown?email=amrinder@odz.com 有多个内部重定向。

在每个重定向中,它都会对已经编码的电子邮件进行编码。

以下是对导致问题的确切原因的简要说明:

At first, @ is passed as it is.

During first redirection, it is encoded to %40 which is still valid.

Here after in each redirection, it gets encoded again resulting in %252540 as the final value.

下面是屏幕截图,可以让您清楚地了解这一点