javascript - 错误:无法在“窗口”上执行“获取”:使用 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 字符串中,都可以正常工作!
解决方案
错误说明问题出在哪里。方法 GET 不能有主体。将数据作为查询字符串放置。
https://maps.googleapis.com/maps/api/timezone/json?location=...×tamp=...
推荐阅读
- r - 如何在 R 中的 scale() 函数之后对每列的标准偏差进行子集化?
- javascript - 需要在随机图像/链接加载脚本的 Javascript 代码上添加登陆到 _blank
- vba - 在重复部分内容控件中向内容控件添加和设置文本
- swift - 保存具有字典的结构
- laravel - api响应错误时如何使所有脚本不运行?
- python - pywinauto print_control_identifiers() 不起作用
- c# - 正方体 OCR 失败 C#
- git - 无法克隆 git repo 并收到错误 ssh_askpass: exec(/usr/bin/ssh-askpass): No such file or directory 主机密钥验证失败
- r - 在 R 中,当 <- 也是赋值时,为什么 rm(list<-ls()) 不起作用?
- mysql - 操作sql表获取一天内超额预定的员工