gpt4 book ai didi

javascript - XMLHttpRequest 在编码之前从 URL 中去除尾随空格

转载 作者:行者123 更新时间:2023-11-30 12:33:08 24 4
gpt4 key购买 nike

在 Windows Chrome 38(可能还有其他浏览器)中,XMLHttpRequest 似乎会自动对 URL 进行编码,但它首先去除尾随空格:

var x = new XMLHttpRequest();
x.open('GET', 'http://example.com/a?b= c d '); // note three spaces
x.send();

在控制台中运行该代码会得到:

GET http://example.com/a?b=%20c%20d 404 (Not Found)

尾随空格已被删除,然后 URL 被编码。

  1. 此行为记录在何处?

我希望它对尾随空格进行编码而不剥离它。

  1. 我应该在调用 XHR.open() 之前自己对 URL 进行编码吗?

最佳答案

Where is this behaviour documented?

XHR's open method使用 basic url parser 解析其 URL 参数, 它 - 在第一步 - 从其输入中去除前导和尾随空格。

Should I be encoding the URL myself before calling XHR.open()?

是的。只需使用 encodeURI在上面:

var x = new XMLHttpRequest();
x.open('GET', encodeURI('http://example.com/a?b= c d '));
x.send();

但也要考虑从一开始就删除这个无效的 url 字符串,即在构造字符串之前。我不确定您从哪里获取参数值,但它可能看起来像

x.open('GET', 'http://example.com/a?b='+encodeURIComponent(…));

关于javascript - XMLHttpRequest 在编码之前从 URL 中去除尾随空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26985146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com