我正在尝试编写一个输入一个单词的代码,点击按钮(稍后我会通过按下“return”按钮来添加代码),并且您会得到10个结果与您输入的单词有关。每个结果都会有一个标题/网址和一个描述(基本上,我认为是页面中的第一句话)。我试图用API Sandbox来找出我需要的东西,但是我得到的结果并不是我想要的。因为他们都返回结果,所以我还是得不到list=search
和generator=search
之间的区别。然而,当我在json中使用list=search
作为data.search [i] .title和说明(?)作为data.search [i] .snippet时,似乎我获得了更多成功。但是,当我使用generator=search
时,我无法致电该信息。 MediaWiki API的文档太混乱了...基本上,我想调用信息的提取,似乎只有在使用generator=search
时才会发生。但是,当我试图将标题称为data.pages[i].title
并将描述称为data.pages[i].extract
时,我收到了错误。关于维基百科api搜索的困惑freecodecamp
这里是我的代码:
$(document).ready(function() {
function getWord() {
var keyword = $("#searchkeyword").val();
$.ajax({
url: "https://en.wikipedia.org/w/api.php?",
type: 'GET',
dataType: "jsonp",
data: {
action: 'query',
format: 'json',
prop: 'extracts',
list: 'search',
srsearch: keyword,
exsentences: '1',
exlimit: '10',
exintro: '1',
\t explaintext: '1'
},
success: function(data) {
console.log(data);
$("#articlearea").empty();
for(var i = 0; i < 10; i++) {
$("#articlearea").append('<div class="articlebox">' + data.query.search[i].title + '<br>' + data.query.search[i].extract + '</div>');
}
}
});
};
$("#submitbtn").on("click", getWord);
});
@import url('https://fonts.googleapis.com/css?family=Amiko:400,600');
body {
position: relative;
font-family: 'Amiko', sans-serif;
}
html, body{
height:100%;
margin: 0;
background-color: #40e0d0;
}
.wrapper {
text-align: center;
position: relative;
}
.container {
width: 75%;
margin: 30px auto 0;
}
.container a {
color: #ffffff;
font-size: 1.1em;
text-decoration: none;
margin-bottom: 10px;
display: block;
}
input {
border: 1px solid #ffffff;
border-radius: 4px;
padding: 5px 8px;
font-size: 1.3em;
}
#submitbtn {
position: relative;
z-index: 1;
left: -32px;
top: -2px;
color: #7B7B7B;
cursor:pointer;
width: 0;
}
.fa-search {
font-size: 1.3em;
}
<div class="wrapper">
<div class="container">
<a id="randomlink" href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">Click here for a random Wikipedia article</a>
<form>
<input id="searchkeyword" type="text" placeholder="Search Wikipedia" />
<i id="submitbtn" class="fa fa-search"></i>
</form>
<div id="articlearea"></div>
</div>
</div>
你能显示你得到什么错误顺便说一句? –
这是我使用'generator = search'时得到的错误:http://codepen.io/kikibres/pen/evZzzX。我仍然可以在控制台中获取数组,但是我也遇到了这个错误:“Uncaught TypeError:无法在控制台中读取'undefined'属性'。 –
请查看我在下面的代码片段中更新的最常见原因 –