【问题标题】:Weather API - Add parameters to HTML form with GETWeather API - 使用 GET 向 HTML 表单添加参数
【发布时间】:2017-08-01 19:47:39
【问题描述】:

我只是想用 GET 提交一个 HTML 表单,以便重定向到另一个 URL。但是,在提交时,GET 方法会添加一个问号。

我使用表单输入作为 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:我如何(使用户能够)在没有问号和 GET 方法的其他“副作用”的情况下向 URL 添加参数?

非常感谢您。

【问题讨论】:

    标签: html forms api url get


    【解决方案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>
    

    【讨论】:

    • 我的荣幸...!
    猜你喜欢
    • 1970-01-01
    • 2016-05-22
    • 2019-12-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多