get - JS 将 XMLHttpRequest 转换为 Fetch 函数 (GET):无法在“窗口”上执行“获取”:使用 GET/HEAD 方法的请求不能有正文
问题描述
如何将旧的基于 XMLHttpRequest 的代码转换为 FETCH 函数?旧代码:
var data = JSON.stringify({
"username": "user",
"token": "12345"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("GET", "http://localhost:5000/v1/data/users/");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(data);
我无法通过 GET 请求发送数据字段
我试过了:
fetch (url, {method: 'GET',
mode:"cors",
body: JSON.stringify({
username,
token
}),
headers: {
'Content-Type': 'application/json'
}
})
但我发现了 Fetch Error: TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body。
解决方案
推荐阅读
- sql-server - 选择列 A 的值(如果不是 SQL Server 中列 B 中的值)
- mysql - 用 MySQL 中的其他 BLOB 替换 BLOB
- sql - 如果表不存在则创建表
- php - Doctrine Query Builder:多个参数化字段,例如 (a.field1, a.field2) IN ((1,1), (2, 2))
- javascript - move div towards mouse at same speed per second
- apache - 如何禁用 Apache ErrorDocument 消息
- nginx - NGINX 正则表达式位置的重写语法是什么?
- sql - 针对一年中的季度或周运行查询
- canvas - Threejs/WebGL 将画布导出为大小图像
- android - 将数据库另存为 CSV 文件