问题
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))
回答1:
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
).
来源:https://stackoverflow.com/questions/44959749/fetch-with-headers