我正在尝试从维基百科api中获取数据。请帮助我理解为什么,在前两行代码中,我确实成功接收了数据,而之后,当我使用JQuery的onclick
时,我没有。
为方便起见,我缩短并简化了代码,只需注意onclick
工作正常。
如需进一步调查,请查看https://codepen.io/Yonatanos/pen/RZadQL
$(function() {
$.getJSON("https://en.wikipedia.org//w/api.php?action=opensearch&search=test&callback=?", function(data) {
console.log(data); // In this case i can see the data
});
$("#send").on("click", function() {
var query = $("#search").val();
console.log(query);
var url = "https://en.wikipedia.org//w/api.php?action=opensearch&search=" + encodeURI(query) + "&callback=?";
console.log(url); //Receiving successful output
console.log('test');
$.getJSON(url, function(data) {
console.log("Testing it"); //No ouput
console.log("data"); //No output
});
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="https://en.wikipedia.org/wiki/Special:Random" target="_blank"><button id="randomSearch">Click here for a random Wiki Article</button></a>
<form action="#" method="get">
<input id="search" type="text" name="search" placeholder="Insert Search Term" autocomplete="off">
<input id="send" type="submit" value="Send">
</form>
<div id="results"></div>