fetch-api

Allow Access-Control-Allow-Origin header using HTML5 fetch API

拈花ヽ惹草 提交于 2019-12-18 11:38:35
问题 I am using HTML5 fetch API. var request = new Request('https://davidwalsh.name/demo/arsenal.json'); fetch(request).then(function(response) { // Convert to JSON return response.json(); }).then(function(j) { // Yay, `j` is a JavaScript object console.log(JSON.stringify(j)); }).catch(function(error) { console.log('Request failed', error) }); I am able to use normal json but unable to fetch the data of above api url. It throws error: Fetch API cannot load https://davidwalsh.name/demo/arsenal.json

Only in Chrome (Service Worker): '… a redirected response was used for a request whose redirect mode is not “follow” '

耗尽温柔 提交于 2019-12-17 19:36:39
问题 When I refresh (or go offline) in Chrome then I get "This site can't be reached" and the following logged to console: The FetchEvent for "http://localhost:8111/survey/174/deployment/193/answer/offline/attendee/240/" resulted in a network error response: a redirected response was used for a request whose redirect mode is not "follow". . When I refresh in Firefox everything works fine. Could someone explain why this is happening? Here is my simplified SW. importScripts("/static/js/libs/idb.js")

fetch API to get HTML response

丶灬走出姿态 提交于 2019-12-17 19:08:40
问题 I am trying to get a page's HTML using fetch API. Here is my code. var quizUrl = 'http://www.lipsum.com/'; var myHeaders = new Headers(); myHeaders.append('Content-Type', 'text/html'); fetch(quizUrl,{ mode: 'no-cors', method: 'get', headers: myHeaders }).then(function(response) { response.text().then(function(text) { console.log(text); }) }).catch(function(err) { console.log(err) }); It returns empty string. Any guesses why it doesn't work? 回答1: About the Request.mode 'no-cors' (from MDN,

Hide 401 console.error in chrome dev tools getting 401 on fetch() call [duplicate]

隐身守侯 提交于 2019-12-17 18:46:33
问题 This question already has answers here : Suppress Chrome 'Failed to load resource' messages in console (3 answers) Closed 2 years ago . I have some code where i make a fetch call. This takes advantage of window.fetch api built into modern chrome / firefox. The code sometimes hits a 401:unauthorized response. This is normal and I want it ignored, which I can do with the flow of the code. However, Chrome does show an unsightly console.error message when I try to run it. How can I

redux fetch body is not use with no cors mode

吃可爱长大的小学妹 提交于 2019-12-17 18:43:13
问题 I have this action which calls a function: dispatch(Api({url: "my_url", method: "POST", data: data})) Here I am passing array as a data.. import fetch from 'isomorphic-fetch' export default function Api({url, method, headers, data}={}){ return dispatch => { console.log(data) console.log(url) console.log(method) console.log(JSON.stringify(data)) let response = fetch(url, { mode: 'no-cors', method: method || null, body: data || null, }).then(function(response) { console.log("response"); console

fetch gives an empty response body

大憨熊 提交于 2019-12-17 18:01:07
问题 I have a react/redux application and I'm trying to do a simple GET request to a sever: fetch('http://example.com/api/node', { mode: "no-cors", method: "GET", headers: { "Accept": "application/json" } }).then((response) => { console.log(response.body); // null return dispatch({ type: "GET_CALL", response: response }); }) .catch(error => { console.log('request failed', error); }); The problem is that the response body is empty in the .then() function and I'm not sure why. I checked examples

Cannot load Deezer API resources from localhost with the Fetch API

做~自己de王妃 提交于 2019-12-17 12:59:13
问题 I'm trying to access the Deezer API from localhost, but I'm keep getting the following error: Fetch API cannot load http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS

Why is the response object from JavaScript fetch API a promise?

痞子三分冷 提交于 2019-12-17 06:08:10
问题 When requesting from a server with JavaScript fetch API, you have to do something like fetch(API) .then(response => response.json()) .catch(err => console.log(err)) Here, response.json() is resolving its promise. The thing is that if you want to catch 404 's errors, you have to resolve the response promise and then reject the fetch promise, because you'll only end in catch if there's been a network error. So the fetch call becomes something like fetch(API) .then(response => response.ok ?

How can I download a file using window.fetch?

孤者浪人 提交于 2019-12-17 04:55:41
问题 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. 回答1: 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

Fetch API request timeout?

百般思念 提交于 2019-12-17 02:28:57
问题 I have a fetch-api POST request: fetch(url, { method: 'POST', body: formData, credentials: 'include' }) I want to know what is the default timeout for this? and how can we set it to a particular value like 3 seconds or indefinite seconds? 回答1: It doesn't have a specified default; the specification doesn't discuss timeouts at all. You can implement your own timeout wrapper for promises in general: // Rough implementation. Untested. function timeout(ms, promise) { return new Promise(function