I started recently working with the fetch library in javascript to execute a few POST/GET calls I need, and am having trouble getting the response data in successful calls.
This is my current code, where I am trying to request an authentication token:
var request = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
username: username,
password: password,
})
}
fetch('http://127.0.0.1:8000/api/login/', request)
.then(function(response) {
if (response.ok) {
console.log("SUCCESS");
} else {
console.log("FAIL");
}
});
Interesting enough, when I send empty strings, which results in a response with error code 400, I get an expected FAIL in the console. The problem happens when I successfully send correct authentication details. In that case, SUCCESS should be printed, but instead nothing is printed out in the console. In the server itself, I am seeing an expected POST request with a 200 response, indicating that authentication was OK.
What am I doing wrong here?