我正在使用duckduckgo API 和jquery,我想显示搜索引擎结果的URL。当我尝试显示摘要和页面标题时,该 API 有效。但是,我无法从搜索引擎结果中显示第一个 URL。这是我的代码:
var b;
function getanswer(q) {
$.get("https://api.duckduckgo.com/?q=" + q + "&format=json", function(a) {
b = JSON.parse(a);
if (b.Abstract == "") {
document.getElementById('answer').innerHTML = "No results found";
} else {
document.getElementById('answer').innerHTML = '<h1>' + b.Heading + "</h1><img src='" + b.Image + "' width='100'><br> Abstract:<br>" + b.Abstract + "<br>";
document.getElementById('urlDiv').innerHTML = "FIRST URL:" + b.Results[0].FirstURL;
}
});
}
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
Enter the query:<input onkeyup="getanswer(document.getElementById('querybox').value)" id='querybox'></input>
<div id='answer'></div>
<div id='urlDiv'></div>