My Django backend is serving JSON data, but I'm encountering some unexpected results. When using curl 127.0.0.1:8000/posts/
, the response includes:
[
{
"title": "This is a title",
"body": "Body :)",
"pub_date":"2020-11-25T13:36:57Z"
},
...
]
However, when running the following JavaScript code:
const API = '127.0.0.1:8000/posts/'
fetch(API).then(response => console.log(response))
The output differs, showing:
Response {
type: "basic",
url: "http://localhost:3000/127.0.0.1:8000/posts/",
redirected: false,
status: 200,
ok: true,
statusText: "OK",
headers: Headers,
body: ReadableStream,
bodyUsed: false
}
This behavior is not as expected. Additionally, trying to parse the response with
.then(response => response.json())
leads to:
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Subsequent attempts such as:
fetch(API).then(response => console.log(response.headers))
fetch(API).then(response => console.log(response.text()))
Only result in:
Headers { }
Promise { "pending "}
<state>: "pending"
And further operations return the same error message regarding JSON parsing.
An update reveals that no new requests are being registered on the Django server log when refreshing the JavaScript page, despite successful GET requests made through curl.