fetch-api

How can i get errors value inside Promise object which return from Fetch Api?

人盡茶涼 提交于 2019-12-06 09:41:09
I have my fetch request like this. fetch(deafaultUrl + '/v1/users/', { headers: { 'Content-Type': 'application/json' }, method: "POST", body: JSON.stringify(userInfoParams) }) .then(function(response) { console.log(response); console.log(response.json()); // not working never go into this function response.json().then(function(value) { console.log('access promise'); console.log(value); // "Success" }); if (response.status !== 200 && response.status !== 201) { throw new Error("Bad response from server"); } }) .then(function(json){ console.log("succeed json re"); console.log(json); dispatch

Request cannot be constructed from a URL that includes credentials

坚强是说给别人听的谎言 提交于 2019-12-06 09:25:35
I want to get a JSON from an API in React.js. I'm tried with axios, superagent and fetch but it's doesn't worked? let token = '****'; let url = 'https://'+ token +'@api.navitia.io/v1/coverage/fr-idf/stop_areas/stop_area%3AOIF%3ASA%3A59491/departures?'; let myInit = { 'method': 'GET' } fetch(url, myInit).then((response)=>{ return response.json(); }).then((data)=> { console.log('ok'); }).catch(function(err){ console.log('Erreur: ' + err); }); Error: "Request cannot be constructed from a URL that includes credentials" I think the error is letting you know the problem is it doesnt accept

Fetch API default cross-origin behavior

百般思念 提交于 2019-12-06 07:16:53
The Fetch Specifications say that the default Fetch mode is 'no-cors' - A request has an associated mode, which is "same-origin", "cors", "no-cors", "navigate", or "websocket". Unless stated otherwise, it is "no-cors". But, I seem to be noticing this behavioral difference between mode: 'no-cors' and an unspecified mode. As demonstrated in this JSFiddle sample , explicitly defining mode as 'no-cors' makes the response inaccessible to the Javascript, while not specifying a mode makes the Response object available to the calling method. Does explicitly specifying the fetch mode work differently

What is the point of request.mode in the fetch API, especially with respect to cors?

你。 提交于 2019-12-05 23:17:24
Looking at the new fetch API, you can specificy a mode field in the request. From Mozilla : The mode read-only property of the Request interface contains the mode of the request (e.g., cors, no-cors, same-origin, or navigate.) This is used to determine if cross-origin requests lead to valid responses, and which properties of the response are readable. And then how to use it: var myHeaders = new Headers(); var myInit = { method: 'GET', headers: myHeaders, mode: 'cors', cache: 'default' }; fetch('flowers.jpg', myInit).then(function(response) { return response.blob(); }).then(function(myBlob) {

React Native Fetch API not returning my calls

本小妞迷上赌 提交于 2019-12-05 20:37:21
Sorry for the joke in the title. I am currently exploring the fetch API in react native, but I have bumped in to some issues which I cannot wrap my head around. So, I am trying to get a message from a server, which I am calling with the fetch API in the following manner: var serverCommunicator = { test: function() { fetch(baseUrl , { method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', } }) .then((response) => response.text()) .then((responseText) => { return (JSON.stringify(responseText)); }) .catch((error) => { console.warn(error); }).done(); }, module

How to return the json response from the fetch API

喜欢而已 提交于 2019-12-05 19:42:38
I have a function like so: check_auth(){ fetch(Urls.check_auth(), { credentials: 'include', method: 'GET' }).then(response => { if(response.ok) return response.json(); }).then(json => { return json.user_logged_in; }); } And then I try to do this: if(this.check_auth()){ // do stuff } else { // do other stuff } But, this.check_auth() is always undefined . What am I missing here? I thought that within fetch's then() was where the resolved Promise object was therefore I thought that I'd get true when the user was logged in. But this is not the case. Any help would be greatly appreciated. Async

Handling authentification to Firebase Database with Fetch in a Service Worker

守給你的承諾、 提交于 2019-12-05 18:30:53
I'm trying to query a Firebase database from a Service Worker using the Fetch API. However it doesn't work as expected as I can't get authenticated correctly. Basically what I'm trying to do is from origin https://myproject.firebaseapp.com inside a Service Worker I do a call like this : var fetchOptions = {}; fetchOptions.credentials = 'include'; var url = options.messageUrl; var request = new Request('https://myproject.firebaseio.com/user/foobar.json', fetchOptions); messagePromise = fetch(request).then(function(response) { return response.json(); }); I'm getting this error : Fetch API cannot

React Native fetch() not working

守給你的承諾、 提交于 2019-12-05 13:09:18
I am trying to create a React Native app which fetches data from Google APIs but I am experiencing some issues (Unhandled JS Exception: undefined is not an object (evaluating 'responseData[0].destination_addresses') ). Here is the code: 'use strict'; var React = require('react-native'); var { AppRegistry, StyleSheet, Text, View, TouchableHighlight, } = React; var INITIAL_DATA = [ {city: 'CityName', duration: "0 hours"}, ]; var REQUEST_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json?mode=driving&language=en&origins=Austin&destinations=San+Francisco&key=PRIVACY'; var

Fetch API cannot load file:///android_asset/www/xx/xxx.json. URL scheme “file” is not supported

北城余情 提交于 2019-12-05 13:05:54
Using cordova build Android app , and add cordova hot code push plugin to make app update automatically, and using Fetch API to load JSON files which located in current project directory, the problem is when update the app, any JSON files cannot be reload,and throw the error Fetch API cannot load file:///android_asset/www/xx/xxx.json. URL scheme "file" is not supported. How to solve this Fecth error in Android app? Or is there any plugin that need add to my cordova project? https://github.com/github/fetch/pull/92#issuecomment-140665932 You may use XMLHttpRequest for loading local assets. I

React Native - mocking FormData in unit tests

女生的网名这么多〃 提交于 2019-12-05 05:27:36
I'm having issues testing my thunks, as many of my API calls are using FormData, and I can't seem to figure out how to mock this in tests. I'm using Jest. My setup file looks like this: import 'isomorphic-fetch'; // Mocking the global.fetch included in React Native global.fetch = jest.fn(); // Helper to mock a success response (only once) fetch.mockResponseSuccess = body => { fetch.mockImplementationOnce(() => Promise.resolve({ json: () => Promise.resolve(JSON.parse(body)) }) ); }; // Helper to mock a failure response (only once) fetch.mockResponseFailure = error => { fetch