0
我正在嘗試使用API從維基百科獲取數據。Javascript維基百科概要提取錯誤
這裏是最初的代碼卡:
$(document).ready(function() {
var searchTerm = document.title;
$.getJSON("https://en.wikipedia.org/w/api.php?action=parse&page=" + searchTerm + '&prop=text&format=json&callback=?', function(json) {
$('#wikiInfo').html(json.parse.text['*']);
$("#wikiInfo").find("a:not(.references a)").attr("href", function() {
return "http://www.wikipedia.org" + $(this).attr("href");
});
$("#wikiInfo").find("a").attr("target", "_blank");
});
});
<title>Fire</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="wikiInfo"> </div>
然而,這給了我整個頁面,我尋找的東西,這將使我維基的只是概要頁。 我在網上看了一下,該解決方案建議是要改變一些初始參數,使JS的樣子:
var searchTerm = document.title;
$.getJSON("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&titles=" +searchTerm +'&callback=?', function(json) {
$('#wikiInfo').html(json.parse.text['*']);
$("#wikiInfo").find("a:not(.references a)").attr("href", function(){ return "http://www.wikipedia.org" + $(this).attr("href");});
$("#wikiInfo").find("a").attr("target", "_blank");
});
不過,現在我面對的錯誤:
Uncaught TypeError: Cannot read property 'text' of undefined
at Object.success (wikis.html:9)
at c (jquery.min.js:3)
at Object.fireWith [as resolveWith] (jquery.min.js:3)
at k (jquery.min.js:5)
at HTMLScriptElement.n.onload.n.onreadystatechange (jquery.min.js:5)
可能有人請幫助我需要改變什麼?我是處理JSON數據和使用API的新手
太感謝你了!這對我有用:) – Appstarter