2017-08-01 57 views
0

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

我使用表单输入作为URL的参数,所以POST不是一个选项(我认为)。同样,表单应该能够从用户那里接受输入,然后根据该输入进行重定向。我正在使用Metaweather API,它需要在/api/location/(woeid)/上结束,因此我不能在GET方法中允许使用问号。目前,除了问号之外,我没有添加任何参数。

有效的URL的例子: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方法添加参数而没有问号和其他“副作用”?

非常感谢您提前。你的问题

回答

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> 
+0

非常感谢你,它完美的作品! – thesystem

+1

我的快乐......! –