先說結果, 瀏覽器中不能帶.
https://github.com/axios/axios/issues/787
- fetch
fetch("/users", {
method: "GET",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Hubot", login: "hubot" })
});
得到報錯:
Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
具有 GET/HEAD 方法的請求不能有 body.
- xhr
var a = new XMLHttpRequest();
a.open("GET", "/");
a.send("testBody");
xhr 沒有成功發送 testBody 參數.
總結
瀏覽器當前的實現是不允許 get 發送 body, 但后端應用通常可以發送. 比如 nodejs 或 postman .
所以, 如果是自己的程序. 最好按規范來,不要在 get 里傳 body
, 如果是第三方API, 無法更改, 那么可以用后端服務轉發一下. 因為前端不能使用 get 方法從瀏覽器里發送 body.