【发布时间】:2022-01-05 14:14:51
【问题描述】:
我在 GET 请求中设置 JSON 数据时遇到问题。 我试过了: 作为 POST 请求(与 POST 请求一起工作)
xhr.open("GET", "http://localhost/test", true);
body = JSON.stringify({"email": "hey@mail.com", "password": "101010"});
xhr.send(body);
作为查询字符串:
var json = {"hello": "world"};
var url = "http://localhost/test?data=" + encodeURIComponent(JSON.stringify(json));
xhr.open("GET", url, true);
xhr.send();
在后端方法上,req.json 返回 null,但我可以看到查询字符串。
此外,如果我将 JSON 数据设置为正文,它可以在 Postman 中使用。在后端,我在请求中看到 JSON 数据。
P.S.:在我之前的项目中,我使用了相同的后端框架,但前端是基于 jQuery 而不是纯 JS,并且ajax 方法可以正常工作。
【问题讨论】:
-
后端不是
req.params.data吗? -
@evolutionxbox,没有。
req.params- 来自表单的数据 -
@evolutionxbox,现在检查一下。我设置了 3 行:
req.json、req.queryString、req.params。输出:null、data=%7B%22Hello%22%3A%5B1%2C2%2C3%5D%7D、[] -
req.query.data呢?
标签: javascript json xmlhttprequest