簡體   English   中英

Fetch 返回未授權和未定義的

[英]Fetch returns unathorized and undefined

我正在嘗試使用 fetch 訪問 API 但是我在控制台中收到錯誤:

XHR GET https://api.geoapify.com/v1/geocode/search?street=buenos%20aires&city=barcelona&country=espa%C3%B1a&lang=es&limit=1&format=json&apiKey=hereIputmyrealkey 
Estado 401 Unauthorized 
Versión HTTP/2 
Transferido1,52 KB (tamaño 97 B) 
Política de referenciastrict-origin-when-cross-origin

提取看起來像這樣:

var requestOptions = {
  method: 'GET',
credentials: "include"
};

let response = await fetch("https://api.geoapify.com/v1/geocode/search?street=buenos aires&city=barcelona&country=españa&lang=es&limit=1&format=json&apiKey=${MYKEY}");

當我添加:

.then(error => console.log(error)

它返回未定義的響應。

我正在研究 cakephp,url 用於獲取工作,cakephp 之外的獲取工作也有效。 我需要在提取中添加一些東西嗎?

(我的代碼中的密鑰是我的實際密鑰,url 給出了 json 格式的響應)。

我嘗試添加:

var requestOptions = {
  method: 'GET',
credentials: "include"
};

  var requestOptions = {
  method: 'GET', 
  mode: 'no-cors',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
};

但它給出了同樣的錯誤......

添加 ${variable} 時使用反引號

let response = await fetch(`https://api.geoapify.com/v1/geocode/search?street=buenos aires&city=barcelona&country=españa&lang=es&limit=1&format=json&apiKey=${MYKEY}`);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM