当前 URL 的 XMLHttpRequest POST 请求的有效 URL 字符串
Valid URL strings for XMLHttpRequest POST request to the current URL
在XMLHttpRequest
的open()
方法中,url
参数的空字符串有效吗?假设我要POST当前页上的一个表单到同一页,就是:
var request = new XMLHttpRequest();
request.open('POST', '');
...实际上等同于:
var request = new XMLHttpRequest();
request.open('POST', window.location.href);
……?在这种情况下,url
参数使用 ''
和 window.location.href
之间有什么区别吗?
This answer 说空字符串不起作用,但没有解释原因(并且它 确实 通过我自己的实验起作用)。我在文档中找不到明确的答案。
我的理解是 url
,如果是相对的,则由浏览器与当前基数 URL 连接。因此 ''
空字符串应该和 window.location.href 一样有效。见 https://www.w3.org/TR/XMLHttpRequest/#the-open()-method
在XMLHttpRequest
的open()
方法中,url
参数的空字符串有效吗?假设我要POST当前页上的一个表单到同一页,就是:
var request = new XMLHttpRequest();
request.open('POST', '');
...实际上等同于:
var request = new XMLHttpRequest();
request.open('POST', window.location.href);
……?在这种情况下,url
参数使用 ''
和 window.location.href
之间有什么区别吗?
This answer 说空字符串不起作用,但没有解释原因(并且它 确实 通过我自己的实验起作用)。我在文档中找不到明确的答案。
我的理解是 url
,如果是相对的,则由浏览器与当前基数 URL 连接。因此 ''
空字符串应该和 window.location.href 一样有效。见 https://www.w3.org/TR/XMLHttpRequest/#the-open()-method