我是编程和 Web 开发新手,所以我不知道如何使用正确的 Ajax/JSON。我正在使用 HTML 和 jQuery。我创建了文本输入字段,下面有一个提交按钮,我希望我的页面搜索此输入字段中的值并返回结果。
我找到了这个 jQuery 代码来做到这一点,但我不明白 url 的格式。
$("#search").click(function(){
var searchTerm = $("#searchTerm").val();// value entered by the user
var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+ searchTerm + "&format=json&callback=?"; // url to look for using the search input by the user
$.ajax({
type:"GET",
url:url,
async:true,
dataType: "json",
success:function(data){
console.log(data[1][0]);
console.log(data[2][0]);
console.log(data[3][0]);
},
error: function(errorMessage){alert("Error");}
});
});
Run Code Online (Sandbox Code Playgroud)
输入字段是这样的:
<input class="form-control" id="searchTerm">
<button id="search" type="button" class="btn btn-primary">Submit</button>
Run Code Online (Sandbox Code Playgroud)