【发布时间】:2016-11-25 15:40:09
【问题描述】:
我正在使用 HTML、Javascript 和引导程序来构建 Web 应用程序并遇到了困难,我尝试使用 document.getElementById("input here") 但它只返回了一个 0 数组。我想从 api 获取数据,然后将其显示在媒体列表,我尝试使用搜索栏作为过滤 api GET 的一种方式,因为我读到了这在我使用的 api 中是可能的。
$(document).ready(function(){
$("button").click(function(){
var word = document.getElementById("sbar");
$.ajax({
url: 'https://community-food2fork.p.mashape.com/search?key=0ee5a01caf7f7c3512b54978628f1a4e&q=' + word, // The URL to the API. You can get this in the API page of the API you intend to consume
type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc
data: {}, // Additional parameters here
dataType: 'json',
success: function(data) {
search = JSON.parse(JSON.stringify(data.recipes));
console.dir(search);
},
error: function(err) {
alert(err);
},
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "KEY HERE"); // Enter here your Mashape key
}
});
});
输入的 HTML:
<div class="container">
<div style="background: rgba(60, 255, 60, 0.2);" class="jumbotron">
<h1>Meal Manager</h1>
<p class="lead">A simple recipe app that provides ingerdiants fast.</p>
<div class="input-group">
<input type="text" id="sbar" class="form-control" aria-label="...">
<div class="input-group-btn">
<button type="button" class="btn btn-default" id="button" title="Search Database">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
<button type="button" class="btn btn-default btn" title="Sort by reviews" >
<span class="glyphicon glyphicon-star" aria-hidden="true"></span>
</button>
<button type="button" class="btn btn-default btn" title="Sort by trending" >
<span class="glyphicon glyphicon-hand-up" aria-hidden="true"></span>
</button>
</div>
</div>
【问题讨论】:
-
如果你的
input有id="sbar",它应该是:document.getElementById("sbar").value但是如果document.getElementById("sbar")返回一个空数组,那么你的 id 可能是错误的......你可以编辑添加一些HTML 也是? -
刚刚添加到那里
标签: javascript html json twitter-bootstrap