fetch-api

How to fetch XML with fetch api

独自空忆成欢 提交于 2019-11-27 04:26:54
问题 I'm trying to making a weather app that displays the weather and the temperature of many days of the week. I'm currently using openweathermap api for such task, the thing is that the information that I want (that is the date of the weather) only comes in xml format. Since I'm rebuilding it in ES6(ES2015) for academic reasons I wanted to also use the fetch api but since the fetch method parses it, it just delivers an error. so how can i fetch it or mby there is a better way to do it. let apis

Using an authorization header with Fetch in React Native

▼魔方 西西 提交于 2019-11-27 02:39:16
I'm trying to use fetch in React Native to grab information from the Product Hunt API. I've obtained the proper Access Token and have saved it to State, but don't seem to be able to pass it along within the Authorization header for a GET request. Here's what I have so far: var Products = React.createClass({ getInitialState: function() { return { clientToken: false, loaded: false } }, componentWillMount: function () { fetch(api.token.link, api.token.object) .then((response) => response.json()) .then((responseData) => { console.log(responseData); this.setState({ clientToken: responseData.access

try..catch not catching async/await errors

不羁的心 提交于 2019-11-27 02:08:47
Perhaps I misunderstood how catching errors with async/await is supposed to work from things articles like this https://jakearchibald.com/2014/es7-async-functions/ and this http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html , but my catch block is not catching 400/500. async () => { let response try { let response = await fetch('not-a-real-url') } catch (err) { // not jumping in here. console.log(err) } }() example on codepen if it helps 400/500 is not an error, it's a response. You'd only get an exception (rejection) when there's a network problem. When the server answers, you

Returning HTML With fetch()

痴心易碎 提交于 2019-11-27 00:42:39
问题 I'm trying to fetch a file and return it's HTML. However it's not as simple as I'd have imagined. fetch('/path/to/file') .then(function (response) { return response.body; }) .then(function (body) { console.log(body); }); This returns an object called ReadableByteStream . How do I use this to grab the HTML file content? If I change the contents of /path/to/file to be a JSON string, and change the above to: fetch('/path/to/file') .then(function (response) { return response.json(); }) .then

How can I download a file using window.fetch?

谁说胖子不能爱 提交于 2019-11-27 00:38:34
If I want to download a file, what should I do in the then block below? function downloadFile(token, fileId) { let url = `https://www.googleapis.com/drive/v2/files/${fileId}?alt=media`; return fetch(url, { method: 'GET', headers: { 'Authorization': token } }).then(...); } Note the codes are in client-side. Mariusz Pawelski 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

using a fetch inside another fetch in javascript

孤人 提交于 2019-11-27 00:04:14
问题 I want to get an api and after that call another one. Is it wisely using a code like this in javascript? fetch(url, { method: 'get', }).then(function(response) { response.json().then(function(data) { fetch(anotherUrl).then(function(response) { return response.json(); }).catch(function() { console.log("Booo"); }); }); }) .catch(function(error) { console.log('Request failed', error) }); 回答1: Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in

fetch post with multipart form data

我的梦境 提交于 2019-11-26 23:55:52
I am fetching a URL like this: fetch(url, { mode: 'no-cors', method: method || null, headers: { 'Accept': 'application/json, application/xml, text/plain, text/html, *.*', 'Content-Type': 'multipart/form-data' }, body: JSON.stringify(data) || null, }).then(function(response) { console.log(response.status) console.log("response"); console.log(response) }) My API expects the data to be of multipart/form-data so I am using content-type of this type... But it is giving me a response with status code 400. What's wrong with my code? rossipedia You're setting the Content-Type to be multipart/form-data

Fetch API vs XMLHttpRequest

瘦欲@ 提交于 2019-11-26 23:47:09
问题 I know that Fetch API uses Promise s and both of them allow you to do AJAX requests to a server. I have read that Fetch API has some extra features, which aren't available in XMLHttpRequest (and in the Fetch API polyfill, since it's based on XHR ). What extra capabilities does the Fetch API have? 回答1: There are a few things that you can do with fetch and not with XHR: You can use the Cache API with the request and response objects; You can perform no-cors requests, getting a response from a

When do browsers send the Origin header? When do browsers set the origin to null?

蹲街弑〆低调 提交于 2019-11-26 22:52:13
问题 As you can see from this Bugzilla thread (and also), Firefox does not always send an Origin header in POST requests. The RFC states that it should not be sent in certain undefined "privacy-sensitive" contexts. Mozilla defines those contexts here. What I'd like to know is whether these are the only situations in which Firefox will not send the Origin header. As far as I can tell it also will not send it in cross-origin POST requests (though Chrome and IE will), but I can't confirm that in the

What causes a Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': 'only-if-cached' can be set only with 'same-origin' mode error?

假装没事ソ 提交于 2019-11-26 22:15:52
问题 After upgrading to Chrome 64, I realized that this error appears when I load my page on a new tab. I can't identify where it is on the service worker. Here is my code to run the fetch: self.addEventListener('fetch', function(event) { if (event.request.url.startsWith(self.location.origin)) { event.respondWith( caches.match(event.request).then(function(response) { return response || fetch(event.request).then(function(fetch_resp){ return fetch_resp; }); }) ); } }); Could anyone here, who has