错误:无法在“Window”上执行“fetch”:使用 GET/HEAD 方法的请求不能有正文[重复]

问题描述 投票:0回答:1

我正在使用 fetch 来调用 Google TimeZone。但是当我尝试将 lat/lng 和时间戳作为正文参数传递时,出现错误。

错误:无法在“窗口”上执行“获取”:使用 GET/HEAD 方法的请求不能有正文

这是我的代码。 Google 允许这样称呼吗?

const data = new FormData();
data.append('location', this.latitude.value + ',' + this.longitude.value);
data.append('timestamp', Math.floor(Date.now() / 1000).toString());
data.append('key', 'my google API key')
const timeZoneResult = await fetch('https://maps.googleapis.com/maps/api/timezone/json', {
  method: 'GET',
  headers: {},
  body: data,
});
const timeZone = await timeZoneResult.json();

这样,所有的内容都在 url 字符串中,工作正常!

https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810%2C-119.6822510×tamp=1331161200&key=myKeyHere

javascript google-maps-api-3 fetch-api google-maps-timezone
1个回答
10
投票

错误指出问题出在哪里。方法 GET 不能有主体。将数据作为查询字符串放置。

https://maps.googleapis.com/maps/api/timezone/json?location=...&timestamp=...
© www.soinside.com 2019 - 2024. All rights reserved.