I am facing an issue with setting JSON data in a GET request. I have tried the following: Using it as a POST request (it works fine when used as a POST request)
xhr.open("GET", "http://localhost/test", true);
body = JSON.stringify({"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="264e435f664b474f4a0845494b">[email protected]</a>", "password": "101010"});
xhr.send(body);
Trying as a query string:
var json = {"hello": "world"};
var url = "http://localhost/test?data=" + encodeURIComponent(JSON.stringify(json));
xhr.open("GET", url, true);
xhr.send();
In the backend method, req.json
returns null, even though I can see the query string.
Interestingly, it works in Postman if I set JSON data to the body. The backend shows that the JSON data is part of the request.
P.S.: In a previous project where I used the same backend framework but jQuery for the frontend instead of pure JS, the ajax
method worked without any issues.