【问题标题】:Why does restcountries API does not work while my Javascript code is correct为什么当我的 Javascript 代码正确时,restcountries API 不起作用
【发布时间】:2021-11-22 20:22:27
【问题描述】:

我正在尝试通过使用 Github 存储库 https://github.com/public-apis/public-apis 中的链接获取有关使用 Rest Country API 的国家/地区的数据。 当我打开这个 API 这就是我得到的。 {"message":"Page Not Found","_links":{"self":{"href":"/","templated":false}}}

这是我的 Javascript 代码

  const getCountryData = function (country) {
  const request = new XMLHttpRequest();
  request.open('GET', `https://restcountries.eu/rest/v2/name/${country}`);
  request.send();
  request.addEventListener('load', function () {
    console.log(this.responseText);
  });
};

getCountryData('rwanda');
getCountryData('brazil');

这是怎么回事???

【问题讨论】:

  • 当我点击 API 列表中的 REST Countries 时,我会登陆 restcountries.com,上面写着“页面未找到”。但是,当我尝试从 Postman (restcountries.eu/rest/v2/name/rwanda) 甚至直接在浏览器中调用 API 时,它似乎可以正常工作,它只是要求我提供 API 密钥。
  • 你需要钥匙吗?

标签: javascript html css api sass


【解决方案1】:

当我尝试打开 restcountries.eu 站点时,它会将我重定向到这个 http://countrylayer.com/ 站点,也许他们已经更改了域,或者如果你无法弄清楚,那么你可以查看这个站点,看看它是否对你有帮助你想要什么:

https://github.com/samayo/country-json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-28
    • 2021-09-30
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    相关资源
    最近更新 更多