-2

I have a problem with setting JSON data in a GET request. I tried: As POST request (with POST request it works)

xhr.open("GET", "http://localhost/test", true);
body = JSON.stringify({"email": "hey@mail.com", "password": "101010"});
xhr.send(body);

As query string:

var json = {"hello": "world"};
var url = "http://localhost/test?data=" + encodeURIComponent(JSON.stringify(json));
xhr.open("GET", url, true);
xhr.send();

On the backend method, req.json returns null, but I can see the query string.

Also, it works in Postman if I set JSON data to the body. On the backend, I see JSON data in the request.

P.S.: In my previous project I used the same backend framework but the frontend was based on jQuery instead of pure JS and the ajax method worked correctly.

1 Answers1

-2

2 months without web development born such stupid questions. I don't use node.js but I think node.js acts so. I remembered the right way. I have tried to use "localhost?data={a: 9}" but the correct way is "localhost?a=9". The backend will parse all query variables as input in the Rest interface. Thanks to @evolutionxbox for kicking to the right side.