fetch-api

How to get File upload progress with fetch() and WhatWG streams

好久不见. 提交于 2019-12-21 07:35:16
问题 Note: I'm not looking for any alternatives. I know this can be done with XMLHttpRequest. I also don't care about browser support. I just want learn about the new/upcoming standards. I have a File object and I can upload it with PUT using fetch like this: fetch(url, { method: "PUT", body: fileObject, }); How can I get upload progress from this? From what I understand the body of the fetch options can be a ReadableStream. So maybe there is a way to wrap the File object to a ReadableStream and

How to get headers of the response from fetch

限于喜欢 提交于 2019-12-21 03:19:46
问题 I am using fetch on chrome Version 52.0.2743.82 (64-bit). I want to get all the headers in the response. Following snippet only return content-type but if you peek into chrome dev tools it shows many other response headers. How to get other headers from fetch. fetch('https://httpbin.org/get') .then(response => { const headers = response.headers.entries(); let header = headers.next(); while (!header.done){ console.log(headers.value); header = header.next(); } }) I tried polyfilling(manually

fetch not defined in Safari (ReferenceError: Can't find variable: fetch)

梦想的初衷 提交于 2019-12-20 09:16:43
问题 For some reason fetch (https://fetch.spec.whatwg.org/) is not defined in Safari (Version 9.0.3), does anyone know why? It seems to be the standard and works fine in Chrome and Firefox. Can't seem to find anyone else having the same issue I am using react with redux and here is some example code: export function fetchData (url) { return dispatch => { dispatch(loading()) fetch(url, { method: 'GET' }) .then(response => { response.json() .then(data => { dispatch(success(data)) }) }) } } 回答1: You

Error when accessing API with fetch while setting mode to 'no-cors' [duplicate]

霸气de小男生 提交于 2019-12-20 07:23:51
问题 This question already has answers here : Handle response - SyntaxError: Unexpected end of input when using mode: 'no-cors' (5 answers) Closed last year . When trying to resolve a fetch promise with JS is set the mode to 'no-core' based on this answer. However setting the mode to 'corse' results in having: Access to fetch at '{endpoint}' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an

IBM Watson Conversation API: “Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response”

北战南征 提交于 2019-12-20 05:34:04
问题 I created a React-Native app that connected to the Watson REST APIs. Using the fetch library that is part of the ReactNative, everything was working well for getting the Workspaces list, like this: const myAuth = new Buffer(USR+':'+PWD).toString('base64'); const myInit = { method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Origin': '', 'Authorization': 'Basic ' + myAuth, } }; return fetch(URL, myInit) .then((response) => response.json()) .then(

How to include both a parsed response and original response headers in fetch errors [duplicate]

瘦欲@ 提交于 2019-12-19 10:23:50
问题 This question already has answers here : How do I access previous promise results in a .then() chain? (17 answers) Closed 2 years ago . I have the following promise chain: return fetch(request) .then(checkStatus) .then(response => response.json()) .then(json => ({ response: json })) .catch(error => ({ error })) Where checkstatus() checks if the request was successful, and returns an error if it wasn't. This error will be caught and returned. But, the problem is that I want to add the both

Fetch local JSON file from public folder ReactJS

大憨熊 提交于 2019-12-19 06:59:47
问题 I have a problem since two days; I want read a local JSON from my public folder on my React application created with react-app. This is my project structure: public data mato.json (my .JSON file) src components App.js Why I put my file in public folder? If I build my project with file in src folder, my file will be include in the generated main.js by the command yarn build . I want modify my json file without always rebuild my app. So I can't use code like: import Data from './mato.json' …or:

Fetch local JSON file from public folder ReactJS

前提是你 提交于 2019-12-19 06:59:11
问题 I have a problem since two days; I want read a local JSON from my public folder on my React application created with react-app. This is my project structure: public data mato.json (my .JSON file) src components App.js Why I put my file in public folder? If I build my project with file in src folder, my file will be include in the generated main.js by the command yarn build . I want modify my json file without always rebuild my app. So I can't use code like: import Data from './mato.json' …or:

React Fetch not working in IE11

限于喜欢 提交于 2019-12-18 14:19:11
问题 I have a ReactJS application that works as expected in Chrome, but fails in IE-11. The problem is this - we have two drop down lists which are populated from rest services when the page is first loaded. The application is running under SSL. When the page is loaded through IE-11, I get an IE-11 bug issue where the first request call gets cancelled out by the second-the bug is described here: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/1282036/ So, I am just asking the

Proper Django CSRF validation using fetch post request

放肆的年华 提交于 2019-12-18 13:38:29
问题 I'm trying to use JavaScript's fetch library to make a form submission to my Django application. However no matter what I do it still complains about CSRF validation. The docs on Ajax mentions specifying a header which I have tried. I've also tried grabbing the token from the templatetag and adding it to the form data. Neither approach seems to work. Here is the basic code that includes both the form value and the header: let data = new FormData(); data.append('file', file);; data.append(