如果我使用表单发布到 URL(将其更改为 https),则会收到 404 错误(尽管地址栏显示正确地址):
//This ends up at a 404 page (BUT showing the correct address in the address bar)
form.setAttribute( "action", window.location.href.replace( "http:", "https:" ) );
form.submit();
如果我使用 window.location
执行此操作,它会正确显示页面:
//This shows the page correctly
window.location = window.location.href.replace( "http:", "https:" );
即使将表单更改为 method="GET"
也会导致 404 错误
为什么第一个会导致 404 错误?
最佳答案
首先,您向服务器发送一个它不希望接收的表单
在第二个示例中,您只需更改地址栏中的 url,该地址仅发送 http(s) get
关于javascript - 为什么当我将表单发布到 URL 时会收到 404 错误,而使用 window.location 去那里则不会收到 404 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25818735/