如何通过 http 请求使用 api 密钥从简单的 link 访问数据

how to access data from simple link with api key through http request

我有这个简单的link https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key

效果很好,给我这个

{
  "kind": "blogger#blog",
  "id": "##############",
  "name": "blog-blogger",
  "description": "",
  "published": "2022-02-08T08:38:50-08:00",
  "updated": "2022-02-22T16:52:43-08:00",
  "url": "http://original-1-1.blogspot.com/",
  "selfLink":
  "posts": {
    "totalItems": 1
  },
  "pages": {
    "totalItems": 0
  },
  "locale": {
    "language": "en",
    "country": "?",
    "variant": ""
  }
}

现在我如何使用 javascript

获取以上数据

喜欢

l = fetch(https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key);
q = l.json();
console.log(q.kind);

我正在做类似的事情这是我正在使用的代码。

var request = new XMLHttpRequest()

// Open a new connection, using the GET request on the URL endpoint
request.open('GET', `https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key`, true)

request.onload = function () {
  // Begin accessing JSON data here
  var data = JSON.parse(this.response)
  console.log(data) 
}

另一种选择是像您建议的那样使用 fetch

function getData() {
  const response = await fetch('https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key')
  const data = await response.json()
}

这是我从中获取信息的网站: https://www.taniarascia.com/how-to-connect-to-an-api-with-javascript/

希望这能回答您的问题!

捷运

这会让你 kind

const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
    let responseJSON = JSON.parse(this.responseText);
        console.log(responseJSON.kind);
}
xhttp.open("GET", "https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key", true);
xhttp.send();

// 像这样声明一个变量来保存你的 API url const url = " https://www.googleapis.com/blogger/v3/blogs/my-ID?key=Api_key"

 // the spelling of your fetch is incorrect. You can use this

获取(url) .then(响应=> response.json()) .then(数据=> console.log(数据));

// 这应该有效

// 使用你的 API 键 确保将 API 密钥作为查询参数传递给 REST API 调用,方法是将 API_KEY 替换为 API 密钥