Javascript 从Wikipedia获取Api结果-错误

Javascript 从Wikipedia获取Api结果-错误,javascript,api,fetch,wikipedia,Javascript,Api,Fetch,Wikipedia,我必须编写一个代码,从维基百科获取信息,我写了以下内容: function getInfo() { let search = "Teatr Wielki"; fetch("https://en.wikipedia.org/w/api.php?&origin=*&action=opensearch&search=" + search + "&limit=5", { headers: { 'Accept': '

我必须编写一个代码,从维基百科获取信息,我写了以下内容:

function getInfo() {
    let search = "Teatr Wielki";

    fetch("https://en.wikipedia.org/w/api.php?&origin=*&action=opensearch&search=" + search + "&limit=5", {
        headers: {
            'Accept': 'application/json',
        },
    }).then(response => response.json())
      .then(addInfo)
      .catch (e => requestError(e));

    function addInfo(data) {
        console.log(data);
        const info = data.results[3][1];
        if (info) {
            document.querySelector('#results').innerHTML = info;
        } else {
            document.querySelector('#results').innerHTML = 'Unfortunately, no info was returned for this data.'
        }
    }

    function requestError(e, part) {
        console.log(e);
        document.querySelector('#results').innerHTML = '<br>Oh no! There was an error making a request for this place';
    }
}
函数getInfo(){ let search=“Teatr Wielki”; 取回(“https://en.wikipedia.org/w/api.php?&origin=*&action=opensearch&search=“+search+”&limit=5”{ 标题:{ “接受”:“应用程序/json”, }, }).then(response=>response.json()) 。然后(addInfo) .catch(e=>requestError(e)); 函数addInfo(数据){ 控制台日志(数据); const info=data.results[3][1]; 如果(信息){ document.querySelector(“#results”).innerHTML=info; }否则{ document.querySelector(“#results”).innerHTML=“很遗憾,没有为此数据返回任何信息。” } } 函数请求错误(e,部分){ 控制台日志(e); document.querySelector(“#results”).innerHTML=“
哦,不!请求此位置时出错”; } } 但此代码返回一个错误:

TypeError:无法在addInfo读取未定义的属性“3”

我试着使用了很多选项,例如:只有[0],只有[1]——但什么都不起作用。也许你们中的一些人知道我的代码出了什么问题?
感谢您的提示。

从维基百科(mediawiki)api返回的数据没有
结果
键。来自服务器的响应是单个阵列

因此,要获取结果,在代码中,必须按下
results
键并直接访问数据对象:

const info = data[3][1];

您的工作代码:

只需检查响应,您就会看到问题所在。嗨,mrlew!非常感谢您提供的信息!有效:)谢谢!