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

问题描述

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

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

这是我的代码。谷歌允许这样称呼它吗?

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

标签: javascriptgoogle-maps-api-3fetchgoogle-maps-timezone

解决方案


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

https://maps.googleapis.com/maps/api/timezone/json?location=...&timestamp=...

推荐阅读