gpt4 book ai didi

javascript - 为什么当我将表单发布到 URL 时会收到 404 错误,而使用 window.location 去那里则不会收到 404 错误?

转载 作者:行者123 更新时间:2023-11-28 07:58:06 25 4
gpt4 key购买 nike

如果我使用表单发布到 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/

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