Reputation: 105
When use fetch for request in server using headers return
SyntaxError: Unexpected end of input
at index.js:50
at <anonymous>
Line of code 50 is }).then(res => res.json())
What can be wrong?
This code fetch.
fetch(api-url, {
mode: 'no-cors',
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': ' application/json',
'X-API-SERVER': '85499f9f'
},
}).then(res => res.json())
.then(res => {
if (res.status === 200){
console.log("accepted");
}else {
console.log(res.error);
}
console.log(res.error)
}).catch(err => console.log(err))
Upvotes: 5
Views: 15730
Reputation: 482
With res.status
you're trying to retrieve the status
property from the response body (as returned by the json() function), not the response object itself.
Upvotes: 0
Reputation: 18556
Since you're requesting an API, you don't want to disable CORS. It's probably enough to just remove mode: 'no-cors'
in your fetch
request to fix this, as long as your API server sends the correct headers as well (Access-Control-Allow-Origin
).
Upvotes: 3