使用包含“+”的令牌参数重定向 URL
Redirecting URL With Token Parameter That Contains '+'
我在从我在应用程序中使用的 API 创建到 URL 的重定向时遇到问题。我请求在重定向 URL 中使用的 session token
,但当令牌中包含 +
时,我遇到了问题。我的猜测是浏览器将其作为 space
而不是导致重定向经常失败的实际令牌字符。
这是一个可以从请求返回到 API 的示例令牌:1w5OX65MRj+3J9R5AXjMWQLAAXIo5TXa
查看网络选项卡,我看到它尝试使用此令牌请求重定向:
1w5OX65MRj 3J9R5AXjMWQLAAXIo5TXa
,这就是它导致问题的原因。
我尝试用 %2B
替换 +
但我的应用程序似乎根本没有替换它,这对我来说有点奇怪。
这是我的代码:
let token = "";
$.get('/_token_req', {contentType: "application/x-www-form-urlencoded"}, (response) => {
//console.log(response);
token = response;
token = token.replace(/\+/g, "%2B"); // this doesn't replace the + character for some reason
$.get('/_redirect', {token: response}, (response) => {
//console.log(response);
if(response == "OK"){
window.location.href = "https://someapi/payments/?auth_token=" + token;
}
})
})
我对 URL 编码了解不多,但如果有人能为我指出正确的方向,那将非常有帮助。谢谢!
您的代码有 2 个问题:
您需要使用 encodeURIComponent
对令牌中的任何符号进行编码,以便以适当的方式发送。
第二,在调用第二个请求 $.get('/_redirect'
时,您没有使用 replace
d 令牌,而是使用从先前请求收到的简单 response
。
将您的代码更改为此以消除这两个错误:
let token = "";
$.get('/_token_req', {contentType: "application/x-www-form-urlencoded"}, (response) => {
token = encodeURIComponent(response);
$.get('/_redirect', {token: token}, (response) => {
if(response == "OK"){
window.location.href = "https://someapi/payments/?auth_token=" + token;
}
})
})
我在从我在应用程序中使用的 API 创建到 URL 的重定向时遇到问题。我请求在重定向 URL 中使用的 session token
,但当令牌中包含 +
时,我遇到了问题。我的猜测是浏览器将其作为 space
而不是导致重定向经常失败的实际令牌字符。
这是一个可以从请求返回到 API 的示例令牌:1w5OX65MRj+3J9R5AXjMWQLAAXIo5TXa
查看网络选项卡,我看到它尝试使用此令牌请求重定向:
1w5OX65MRj 3J9R5AXjMWQLAAXIo5TXa
,这就是它导致问题的原因。
我尝试用 %2B
替换 +
但我的应用程序似乎根本没有替换它,这对我来说有点奇怪。
这是我的代码:
let token = "";
$.get('/_token_req', {contentType: "application/x-www-form-urlencoded"}, (response) => {
//console.log(response);
token = response;
token = token.replace(/\+/g, "%2B"); // this doesn't replace the + character for some reason
$.get('/_redirect', {token: response}, (response) => {
//console.log(response);
if(response == "OK"){
window.location.href = "https://someapi/payments/?auth_token=" + token;
}
})
})
我对 URL 编码了解不多,但如果有人能为我指出正确的方向,那将非常有帮助。谢谢!
您的代码有 2 个问题:
您需要使用 encodeURIComponent
对令牌中的任何符号进行编码,以便以适当的方式发送。
第二,在调用第二个请求 $.get('/_redirect'
时,您没有使用 replace
d 令牌,而是使用从先前请求收到的简单 response
。
将您的代码更改为此以消除这两个错误:
let token = "";
$.get('/_token_req', {contentType: "application/x-www-form-urlencoded"}, (response) => {
token = encodeURIComponent(response);
$.get('/_redirect', {token: token}, (response) => {
if(response == "OK"){
window.location.href = "https://someapi/payments/?auth_token=" + token;
}
})
})