Fetch API with Cookie
Fetch does not use cookie by default. To enable cookie, do this: fetch(url, { credentials: “same-origin” }).then(…).catch(…);
Fetch does not use cookie by default. To enable cookie, do this: fetch(url, { credentials: “same-origin” }).then(…).catch(…);
EDIT: syg answer is better. Just use downloadjs library. The answer I provided works well on Chrome, but on Firefox and IE you need some different variant of this code. It’s better to use library for that. I had similar problem (need to pass authorization header to download a file so this solution didn’t helped). … Read more
If an opaque response serves your needs It doesn’t. You want to see the response. You can’t see an opaque response (that is what opaque response means). no-cors mode means that if the browser has to do anything that requires permission from CORS, it will fail silently instead of throwing an error. So it is … Read more
An HTTP 5xx error indicates some failure on the server side. Or it can even indicate the server just isn’t responding at all — e.g., a case might be, your backend tries to proxy a request to a server on another port, but the server is not even be up and listening on the expected … Read more
I’ve done it like this: var input = document.querySelector(‘input[type=”file”]’) var data = new FormData() data.append(‘file’, input.files[0]) data.append(‘user’, ‘hubot’) fetch(‘/avatars’, { method: ‘POST’, body: data })
TL/DR: fetch now supports a signal parameter as of 20 September 2017, but not all browsers seem support this at the moment. 2020 UPDATE: Most major browsers (Edge, Firefox, Chrome, Safari, Opera, and a few others) support the feature, which has become part of the DOM living standard. (as of 5 March 2020) This is … Read more
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
Why does response.json return a promise? Because you receive the response as soon as all headers have arrived. Calling .json() gets you another promise for the body of the http response that is yet to be loaded. See also Why is the response object from JavaScript fetch API a promise?. Why do I get the … Read more
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
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