2017-08-01 47 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

我的快樂......! –