学习HTML和JavaScript,我试图根据通过表单提交的输入来为API请求形成自定义URL。我已经能够制作大多数URL,但是在将URL字符串连接到“?”之后却遇到了问题。提交动作时。我到目前为止仅凭文档和论坛帖子就做到了这一点,但无法弄清楚如何完成此操作,只想问一个人。
<div id="stock_quote_iex">
<form id="get_stock_quote_form" onsubmit="get_stock_quote()" method="GET">
<p>Enter Symbol and API Key</p>
<input id="symbol" type="text" placeholder="Symbol">
<input id="api_key" type="text" placeholder="API_KEY">
<input type="submit" value="Submit">
</form>
</div>
<script language="javascript" type="text/javascript">
function get_stock_quote()
{
var form = document.getElementById('get_stock_quote_form')
var symbol = document.getElementById('symbol').value
var api_key = document.getElementById('api_key').value
var action_src = "https://sandbox.iexapis.com/stable/stock/" + symbol + "/quote?token=" + api_key
form.action = action_src;
}
</script>
Example inputs: symbol = NFLX API_KEY = pk_s0m3rand0mphra5e Expected output: https://sandbox.iexapis.com/stable/stock/NFLX/quote?token=pk_s0m3rand0mphra5e Actual output: https://sandbox.iexapis.com/stable/stock/NFLX/quote?
答案 0 :(得分:1)
已通过从字符串连接中删除查询参数并使用API_Key上的name属性来更正。
<div id="stock_quote_iex">
<form id="get_stock_quote_form" onsubmit="get_stock_quote()" method="GET">
<p>Enter Symbol and API Key</p>
<input id="symbol" type="text" placeholder="Symbol">
<input id="api_key" name="token" type="text" placeholder="API_KEY">
<input type="submit" value="Submit">
</form>
</div>
<script language="javascript" type="text/javascript">
function get_stock_quote()
{
var form = document.getElementById('get_stock_quote_form')
var symbol = document.getElementById('symbol').value
var action_src = "https://sandbox.iexapis.com/stable/stock/" + symbol + "/quote"
form.action = action_src;
}
</script>