我只是想提交一个带有GET的HTML表单,以便重定向到另一个URL。但是,在提交时,GET方法会添加一个问号。
我使用form-input作为URL的参数,因此POST不是一个选项(我认为)。同样,表单应该能够从用户那里获取输入,然后根据它进行重定向。我正在使用Metaweather API,它需要以/api/location/(woeid)/
结尾,因此我不能允许来自GET方法的问号。目前,我没有运气添加问号以外的任何参数。
有效网址示例:https://www.metaweather.com/api/location/44418/(注意:无问号)
HTML
<!DOCTYPE html>
<html>
<body>
<form method="GET" target="_blank" action="https://www.metaweather.com/api/location/">
<input type="text" placeholder="Location..">
</form>
</body>
</html>
期望的结果:如果您在输入中写“44418”,您将被重定向到https://www.metaweather.com/api/location/44418/
注意:我已经有了CORS / XMLHttpRequest / JSON.parse,我只是试图让用户自己提交/搜索一个位置。
TL; DR:我如何(允许用户)向URL添加参数而没有问号和GET方法的其他“副作用”?
非常感谢你。
答案 0 :(得分:1)
解决问题的方法:
<form method="GET" target="_blank"
action="https://www.metaweather.com/api/location/"
onsubmit="location.href = this.action + this.txt.value; return false;">
<input type="text" id="txt" name="txt" placeholder="Location..">
</form>