It is a very simple fetch api but for some reason I dont know why the body is not working. here is the code.
<label id="msg"></label>
<label id="msg2"></label>
<script type="text/javascript">
const myRequest = new Request('some url', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({"email" : "email",
"password" : "password"}),
mode: 'no-cors'});
const myMethod = myRequest.method; // POST
const bodyUsed = myRequest.bodyUsed; // true
fetch(myRequest)
.then(response => {
if (response.status === 200) {
return response.json();
} else {
//throw new Error('Something went wrong on api server!');
}
})
.then(response => {
document.getElementById("msg").innerHTML = response;
document.getElementById("msg2").innerHTML = bodyUsed+" "+myMethod;
}).catch(error => {
document.getElementById("msg").innerHTML = error;
});
</script>
Is there anything I am doing wrong? I have stringify the body, changed the header but still when it runs, it shows false in msg2 label (which is nothing but just the body part of the request, just to be sure). which means the body is actually NaN. where is the problem and whats the solution?