当我向
例如:
因此,最初发出的请求是针对
使用
问题:
- 为什么浏览器会自动更改它?它也会发生在其他URL上。据我所知,添加尾随斜线通常可以解决问题,但总体来说,这是一个破坏性的改变。 - 如果我想在
http://www.example.com
发起请求时,为什么在webRequest.onBeforeRequestListener中看到的是http://www.example.com/
?例如:
chrome.webRequest.onBeforeRequest.addListener(
details => console.log('Sending request to', details.url),
{ urls: ['<all_urls>'] });
fetch('http://www.example.com');
将会打印
Sending request to http://www.example.com/
这与网络请求监视器中显示的请求URL一致。例如,如果我将其转换为curl命令,则请求如下所示:
curl 'http://www.example.com/' -H 'Accept: */*' -H 'Connection: keep-alive'
-H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9'
-H 'User-Agent: ...' --compressed
因此,最初发出的请求是针对
http://www.example.com/
而非http://www.example.com
。这个决定必须是在浏览器中做出的,而非服务器端。使用
XMLHttpRequest
时也会出现同样的行为,与fetch
相同。我使用的是Chrome浏览器,但在Firefox上也是一样的。问题:
- 为什么浏览器会自动更改它?它也会发生在其他URL上。据我所知,添加尾随斜线通常可以解决问题,但总体来说,这是一个破坏性的改变。 - 如果我想在
onBeforeRequest
监听器中过滤当前请求到特定的URL,如何可靠地匹配它?例如,仅检查URL是否相同将失败。
- 浏览器中是否还有更多的重写URL规则需要注意?