I am sending request to WordPress REST API endpoint -
https://example.com/wp-json/wp/v2/comments.
But it is not sending JSON as response. Instead, it is sending HTML having script tag as follows -
<html>
<body>
<script>
document.cookie =
"_test=<cookie value> ; expires=<expiary date>; path=/";
document.location.href =
"https://example.com/wp-json/wp/v2/comments";
</script>
</body>
</html>
Here, 1) a cookie named _test is getting created, 2) After that, it is redirecting to the requested URL with document.location.href.
So, when I am trying to parse the response using JSON.parse method, then it is failing, as it is in HTML format.
But, when I am entering the endpoint URL in browser search bar, then document.location.href method inside the script of the response is helping to redirect to the expected JSON.
My expected response should be like this
[{"id":1,"post":1,"parent":0,"author":0,"author_name":"A WordPress Commenter","author_url":"https:\/\/wordpress.org\/","date":"2022-07-22T16:38:55","date_gmt":"2022-07-22T16:38:55","content":{"rendered":"Comment 1"}}, /*...*/]
Now, how to get response as JSON directly, instead of HTML?