fetch-api

ReactJS: What is the correct way to set a state value as array?

无人久伴 提交于 2019-12-02 03:34:46
I have an array of object that get users data using fetch API. I have tried constructor, create a function, bind it. It didn't work. I tried ComponentDidMount and setState, it returns undefined. class Admin extends Component { componentDidMount () { var that = this; fetch('http://localhost:4500/data/users', { method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', } }).then(function(response) { return response.json(); }).then(function(json){ console.log(json); that.state = {users: json}; }); } render() { return ( <SpicyDatatable tableKey={key} columns=

CORS error with window.fetch on YouTube resource

时光总嘲笑我的痴心妄想 提交于 2019-12-02 02:29:10
问题 I'm using window.fetch to retrieve a YouTube resource with the following URL: http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=[id]&format=json Using Fiddler, I see that I am getting the expected reply from YouTube's servers containing JSON data. However, I'm not able to use it as I'm getting a Javascript error. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:60366' is therefore not allowed access. I tried enabling CORS on

CORS error with window.fetch on YouTube resource

故事扮演 提交于 2019-12-02 01:32:13
I'm using window.fetch to retrieve a YouTube resource with the following URL: http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=[id]&format=json Using Fiddler, I see that I am getting the expected reply from YouTube's servers containing JSON data. However, I'm not able to use it as I'm getting a Javascript error. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:60366 ' is therefore not allowed access. I tried enabling CORS on my window.fetch but I read that this will only work if the server allows CORS in its header response

When should I really set “Access-Control-Allow-Credentials” to “true” in my response headers?

孤街浪徒 提交于 2019-12-02 01:13:57
问题 MDN says, when the credentials like cookies, authorisation header or TLS client certificates has to be exchanged between sites Access-Control-Allow-Crendentials has to be set to true . Consider two sites A - https://example1.xyz.com and another one is B- https://example2.xyz.com. Now I have to make a http Get request from A to B. When I request B from A I am getting, "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example1.xyz.com' is therefore

No JSON object with fetch()

不羁岁月 提交于 2019-12-02 01:11:15
问题 I have a oauth set up. But when I want to get the access token with the fetch() function it just returns an object with things like _bodyInit, _bodyBlob and headers. So I just cannot get a JSON object. I'm on Android if that matters in any way. Code: componentDidMount() { Linking.getInitialURL().then(url => { if(url) { console.log(url); const queries = url.substring(16) const dataurl = qs.parse(queries); if(dataurl.state === 'ungessable15156145640!') { console.log(dataurl.code); console.log

No JSON object with fetch()

北战南征 提交于 2019-12-01 21:44:57
I have a oauth set up. But when I want to get the access token with the fetch() function it just returns an object with things like _bodyInit, _bodyBlob and headers. So I just cannot get a JSON object. I'm on Android if that matters in any way. Code: componentDidMount() { Linking.getInitialURL().then(url => { if(url) { console.log(url); const queries = url.substring(16) const dataurl = qs.parse(queries); if(dataurl.state === 'ungessable15156145640!') { console.log(dataurl.code); console.log(dataurl.state); return code = dataurl.code; } } }).then((code) => { fetch(`https://dribbble.com/oauth

When should I really set “Access-Control-Allow-Credentials” to “true” in my response headers?

蹲街弑〆低调 提交于 2019-12-01 20:19:36
MDN says, when the credentials like cookies, authorisation header or TLS client certificates has to be exchanged between sites Access-Control-Allow-Crendentials has to be set to true . Consider two sites A - https://example1.xyz.com and another one is B- https://example2.xyz.com . Now I have to make a http Get request from A to B. When I request B from A I am getting, "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://example1.xyz.com ' is therefore not allowed access." So, I'm adding the following response headers in B response.setHeader("Access

Replacing $http with Fetch API

给你一囗甜甜゛ 提交于 2019-12-01 20:16:21
问题 I'm replacing $http with Fetch API and got replaced $q with Promise API. Because of that, Angular didn't run digest cycles anymore, thus UI didn't render. To solve this problem I tried Zone.js and that seems to solve our problems partially. Unfortunately its API completely changed in 0.6 so we're using legacy 0.5.15. Now to the actual problem. When refreshing the page Angular configs and bootstraps the application like expected. In this phase I'm initializing the Zone and decorating the

Replacing $http with Fetch API

╄→гoц情女王★ 提交于 2019-12-01 18:40:07
I'm replacing $http with Fetch API and got replaced $q with Promise API. Because of that, Angular didn't run digest cycles anymore, thus UI didn't render. To solve this problem I tried Zone.js and that seems to solve our problems partially. Unfortunately its API completely changed in 0.6 so we're using legacy 0.5.15 . Now to the actual problem. When refreshing the page Angular configs and bootstraps the application like expected. In this phase I'm initializing the Zone and decorating the $rootScope.apply with the Zone and $rootScope.$digest() . Now when I transition between states/routes (with

Can one use the Fetch API as a Request Interceptor?

不羁的心 提交于 2019-12-01 18:16:07
I'm trying to run some simple JS functions after every request to the server with the Fetch API. I've searched for an answer to this question, but haven't found any, perhaps due to the fact that the Fetch API is relative recent. I've been doing this with XMLHttpRequest like so: (function () { var origOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function () { this.addEventListener('load', function () { someFunctionToDoSomething(); }); origOpen.apply(this, arguments); }; })(); Would be great to know if there's a way to accomplish this very same global thing using the