Handle response – SyntaxError: Unexpected end of input when using mode: ‘no-cors’

You need to remove the mode: ‘no-cors’ setting from your request. Setting no-cors mode is exactly the cause of the problem you’re having. A no-cors request makes the response type opaque. The log snippet in the question shows that. Opaque means your frontend JavaScript code can’t see the response body or headers. https://developer.mozilla.org/en-US/docs/Web/API/Request/mode explains: no-cors … Read more

Trying to use fetch and pass in mode: no-cors

mode: ‘no-cors’ won’t magically make things work. In fact it makes things worse, because one effect it has is to tell browsers, “Block my frontend JavaScript code from seeing contents of the response body and headers under all circumstances.” Of course you never want that. What happens with cross-origin requests from frontend JavaScript is that … Read more

Fetch: POST JSON data

With ES2017 async/await support, this is how to POST a JSON payload: (async () => { const rawResponse = await fetch(‘https://httpbin.org/post’, { method: ‘POST’, headers: { ‘Accept’: ‘application/json’, ‘Content-Type’: ‘application/json’ }, body: JSON.stringify({a: 1, b: ‘Textual content’}) }); const content = await rawResponse.json(); console.log(content); })(); Can’t use ES2017? See @vp_art’s answer using promises The question … Read more