fetch-api

React Native fetch() Network Request Failed

一世执手 提交于 2019-12-17 02:10:09
问题 When I create a brand new project using react-native init (RN version 0.29.1) and put a fetch in the render method to the public facebook demo movie API, it throws a Network Request Failed . There is a very useless stack trace and I can't debug network requests in the chrome console. Here is the fetch I'm sending: fetch('http://facebook.github.io/react-native/movies.json') .then((response) => response.json()) .then((responseJson) => { return responseJson.movies; }) .catch((error) => { console

How to Store Fetch API JSON Response in a JavaScript Object

烈酒焚心 提交于 2019-12-14 03:25:01
问题 I want to store a Fetch API JSON as a JavaScript object, so I can use it elsewhere. The console.log test works, but I can't access the data. The Following Works: It shows console entries with three to-do items: fetch('http://localhost:3000/api/todos') .then(data => data.json()) .then(success => console.log(success)); The Following Does Not Work: fetch('http://localhost:3000/api/todos') .then(data => data.json()) .then(success => JSON.parse(success)); If I try to access success, it does not

Fetch API; why use the “no-cors” mode if the response is Opaque?

混江龙づ霸主 提交于 2019-12-13 19:43:07
问题 I read this question and this question . The former only explains 'no-cors' vs 'same-origin'; the latter recommends that 'no-cors' is not useful because of the Opaque Response (which Javascript cannot read/do anything useful with): You basically never ever want to use mode: 'no-cors' in practice — except in some very limited cases. That’s because what setting mode: 'no-cors' actually says to the browser is, “Block my frontend JavaScript code from looking into the contents of the response body

How do you stub fetch api request

我怕爱的太早我们不能终老 提交于 2019-12-13 17:41:37
问题 I am having a problem with stubbing/testing function that is using fetch. Using simple example: export const clickTimeseries => { return fetch('...url...') .then(response => { if(response.status == 200) return response.json() else throw 'something' }) .then(json => { return { success: true, data: json, } }) .catch(err => { return { success: false, error: err, } }) } And my test: import { expect } from 'chai' import sinon from 'sinon' import Promise from 'es6-promise' Promise.polyfill() import

Fetch API get raw value from Response

跟風遠走 提交于 2019-12-13 13:47:25
问题 I use React-Native request some data, here is my code: fetch('https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json') .then((response)=>{ return response.json() }) .then((responseJSON)=>{ callback(responseJSON) }) .catch((error)=>{ console.error(error); }) .done() I see the response is a Response object, and the json function code is return this.text().then(JSON.parse) , I'm confused that what is the parameter of the JSON.parse ? Is that the response raw value

How to download large file with JavaScript

孤人 提交于 2019-12-13 13:16:30
问题 I need to download a large file with JavaScript using XMLHttpRequest or fetch without saving the file first in the RAM-Memory. Normal link download doesn't work for me, because I need to send a Bearer Token in the header of the request. I could manage to download a file, but this "solution", it's saving the file first in the RAM-Memory, before I get a save dialog, so that the Browser will brake if the file is larger then the available RAM-Memory. Here is my "solution" with fetch: var

How to get response data outside promise chain of Fetch? [duplicate]

十年热恋 提交于 2019-12-13 12:48:08
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 2 years ago . I'm using fetchApi to get data from my API. In my RequestHelpers.js, I do this code module.exports = { fetchQuizCollection(){ return fetch(HOST+API_KEY) .then((response) => response.json()) .then((responseData) => { let gameData = responseData console.log(responseData) //It work return responseData }) .done(); } } And I call that function in another file, I couldn

how to trigger webhook from zapier code

旧街凉风 提交于 2019-12-13 08:45:37
问题 I have 2 zaps. First finishes with Code by Zapier block, where I parse input information from previous steps getting array with data e.g.: var elements = [{id: 12, calculatedValue: 13},{id: 13, calculatedValue: 'red'}] then in a loop I traverse it, create requests bodies var options = { "url": "https://hooks.zapier.com/hooks/catch/xxxxxx/xxxxxx/", "method": "POST" }, requests = elements.map(mapDataToSettings); function mapDataToSettings(elem) { var settings = Object.assign({}, options);

React app throws 401 error when accessed through safari, but not otherwise

假如想象 提交于 2019-12-13 03:46:47
问题 I have a react app hosted in an s3 bucket behind a cloudfront distribution. This talks to a django server. Login works correctly on all browsers (confirmation that credentials are correct, and no fundamental connection issues). Once logged in, the app redirects to a page that does a simple GET request via fetch (to a url that requires authentication). This works successfully on all browsers on desktop (macos) except safari, which gets a 401 unauthorised error. Similarly, I get a 401

How to use “this” keyword inside a fetch API call result

心不动则不痛 提交于 2019-12-12 19:19:32
问题 I'm using the following fetch API to get an API result. I need to set the result data in the state but the "this" object is unavailable inside the callback function. How can I update the code to set the value in the state by accessing the this keyword? fetch(config.apiUrl + '/api/user', { headers: authHeader(), method: 'GET', }) .then(function(res) { return res.json(); }) .then(function(data) { this.setState({ id: data.id }); }); 回答1: Assign this keyword to a variable that's outside the