fetch-api

async / await not working in combination with fetch

巧了我就是萌 提交于 2019-11-29 17:08:52
问题 I'm trying to use ES7 async / await together with fetch . I know I'm close but I can't get it to work. Here is the code: class Bar { async load() { let url = 'https://finance.yahoo.com/webservice/v1/symbols/goog/quote?format=json'; try { response = await fetch(url); return response.responseText; } catch (e) { return e.message; } } } which I use as follows: let bar = new Bar(); bar.load().then(function (val) { console.log(val); }); DEMO For some reason I always get into the catch with the

Fetch Api:Can not get data from localhost

孤街浪徒 提交于 2019-11-29 15:25:51
I have been trying to create an android app using nativescript.I am using fetch module to get response from my server.When I am trying to get response from httpbin.org/get ,it is OK.But when I am trying to get response from my local server,I am getting Network Request Failed. error. Sending to httpbin.org/get- return fetchModule.fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) { console.log(r); }, function (e) { console.log(e); }); Sending to localhost:8000/api- return fetchModule.fetch("http://localhost:8000/api").then(response => { return

Django 1.9 AJAX form CSRF token 403 error - “CSRF cookie not set”

末鹿安然 提交于 2019-11-29 14:29:13
问题 I've seen a lot about this on SO, but nothing can fix my problem. Problem: With CSRF middleware enabled, Django responds with 403 on AJAX form request, stating: "CSRF cookie not set." Following the documentation, a JS functionality was implemented, that sets custom "X-CSRFToken" header. It works as expected, gets "csrftoken" cookie from browser and posts it along with AJAX request: x-csrftoken: 1a0u7GCQG0wepZHQNThIXeYpMy2lZOf2 But response is still 403. Tried solutions: I've tried everything

Fetch API to force download file

柔情痞子 提交于 2019-11-29 11:31:41
I'm calling an API to download excel file from the server using the fetch API but it didn't force the browser to download, below is my header response: HTTP/1.1 200 OK Content-Length: 168667 Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet Server: Microsoft-IIS/8.5 Content-Disposition: attachment; filename=test.xlsx Access-Control-Allow-Origin: http://localhost:9000 Access-Control-Allow-Credentials: true Access-Control-Request-Method: POST,GET,PUT,DELETE,OPTIONS Access-Control-Allow-Headers: X-Requested-With,Accept,Content-Type,Origin Persistent-Auth: true X

React Native Fetch does not render response until after clicking screen

十年热恋 提交于 2019-11-29 11:26:47
So I've been building an app that uses the Fetch API to make a request. Unfortunately, I think the code shown in the Facebook docs are (may be?) incorrect. Problem When starting the app, Fetch makes a call to a URL to pull JSON data. componentDidMount(){ return fetch(REQUEST_URL) .then((response) => response.json()) .then((responseJson) => { this.setState({ isLoading: false, data: JSON.stringify(responseJson) }) }) .catch((error) => { console.error(error); }); } This is similar to the code that Facebook docs give as an example. The problem is that when I start the app, the data doesn't render

What is the difference between fetch and jquery ajax?

喜欢而已 提交于 2019-11-29 10:52:59
问题 I want to send a post request through fetch, but it does not work. But if I do it through jQuery ajax, it succeeds. I want to know the difference of the two way and if there is anything wrong in my use of fetch here: fetch('http://localhost:8888/news',{ method:"post", data:"code=7&a=8&b=9" }).then(function(data){ data.json().then(function (json) { } 回答1: Fetch specification differs from jQuery.ajax() in mainly two ways: The Promise returned from fetch() won’t reject on HTTP error status even

Intercept Fetch() API responses and request in Javascript

南楼画角 提交于 2019-11-29 06:31:08
I want to intercept the fetch API request and response in Javascript. For ex: Before sending the request want to intercept the request URL and once get the response wants to intercept the response. The below code is for intercepting response of All XMLHTTPRequest. (function(open) { XMLHttpRequest.prototype.open = function(XMLHttpRequest) { var self = this; this.addEventListener("readystatechange", function() { if (this.responseText.length > 0 && this.readyState == 4 && this.responseURL.indexOf('www.google.com') >= 0) { Object.defineProperty(self, 'response', { get: function() { return bValue;

fetch API to get HTML response

夙愿已清 提交于 2019-11-29 03:14:46
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? About the Request.mode 'no-cors' (from MDN, emphasis mine) Prevents the method from being anything other than HEAD, GET or POST. If any ServiceWorkers

Why is this CORS request failing only in Firefox?

五迷三道 提交于 2019-11-29 03:04:41
I'm implementing CORS with credentials and a preflight request and I'm a bit mystified why the preflight request consistently fails in Firefox 30 but works in Safari (7.0.2) and Chrome 35. I think this issue is different from " Why does the preflight OPTIONS request of an authenticated CORS request work in Chrome but not Firefox? " because I am not getting a 401, but rather a CORS-specific message from the browser client: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myurl.dev.com . This can be fixed by moving the resource to the same

Why is this CORS request failing only in Firefox?

狂风中的少年 提交于 2019-11-29 03:02:53
I'm implementing CORS with credentials and a preflight request and I'm a bit mystified why the preflight request consistently fails in Firefox 30 but works in Safari (7.0.2) and Chrome 35. I think this issue is different from " Why does the preflight OPTIONS request of an authenticated CORS request work in Chrome but not Firefox? " because I am not getting a 401, but rather a CORS-specific message from the browser client: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myurl.dev.com . This can be fixed by moving the resource to the same