fetch-api

How to return return from a promise callback with fetch? [duplicate]

百般思念 提交于 2019-12-29 01:36:11
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 2 years ago . I'm a bit stumped. I've forgotten how to do this. I have a function called ext.get() that takes a parameter of url. It fetches a response from the url. The ext.get() function is meant to return the response as a json. I don't think it is doing that. ext.get = (url) => { let myHeaders = new Headers(); let options = { method: 'GET', headers: myHeaders, mode: 'cors'

Slack incoming webhook: Request header field Content-type is not allowed by Access-Control-Allow-Headers in preflight response

对着背影说爱祢 提交于 2019-12-28 06:00:30
问题 I try to post a slack message via the fetch API in a browser: fetch('https://hooks.slack.com/services/xxx/xxx/xx', { method: 'post', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-type': 'application/json' }, body: JSON.stringify({text: 'Hi there'}) }) .then(response => console.log) .catch(error => console.error); }; I get the following error message: Fetch API cannot load: https://hooks.slack.com/services/xxxxxxx/xxxxx. Request header field Content-type is not allowed by

try..catch not catching async/await errors

大城市里の小女人 提交于 2019-12-28 01:28:31
问题 Perhaps I misunderstood how catching errors with async/await is supposed to work from things articles like this https://jakearchibald.com/2014/es7-async-functions/ and this http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html, but my catch block is not catching 400/500. async () => { let response try { let response = await fetch('not-a-real-url') } catch (err) { // not jumping in here. console.log(err) } }() example on codepen if it helps 回答1: 400/500 is not an error, it's a

Fetch - sending POST data

不问归期 提交于 2019-12-25 06:35:55
问题 My PHP script isn't receiving anything via POST from fetch . What am I doing wrong? Javascript: fetch("http://127.0.0.1/script.php", { method: "post", body: "inputs=" + JSON.stringify({param: 7}) }).then(response => response.json()).then(response => { console.log(response); }); script.php: echo "\nprinting POST:\n"; print_r($_POST); echo "\nprinting GET:\n"; print_r($_GET); die(); Console: printing POST: Array ( ) printing GET: Array ( ) 回答1: You're not adding any url parameters (hence $_GET

JavaScript Fetch API - save text as object?

江枫思渺然 提交于 2019-12-25 01:48:10
问题 I am currently working with the fetch API in JavaScript. I have a text file I'd like to read from called sample.txt. I would like to grab the different lines out of the text file and save it in an array so I can work with it. I've been searching for how to save it as an object, but I think I've been using the code for JSON and not for text. Please give any suggestions? sample.txt apple banana orange grape index.js let fruitArray; //initialized new array fetch('sample.txt') // fetch text file

Getting net::ERR_INSECURE_RESPONSE when calling url using fetch api

六眼飞鱼酱① 提交于 2019-12-24 23:26:37
问题 I'm using fetch API to call a URL which has some SSL settings and after calling it I straight away have to login in order to access the contents and I get this error while calling it net::ERR_INSECURE_RESPONSE my code is let header = new Headers({ 'Access-Control-Allow-Origin':'', 'Content-Type': 'multipart/form-data' }); let username="***"; let password="***"; let url ="***"; let sentData={ method:'POST', mode: 'cors', header: header }; return new Promise((reslove,reject)=>{ fetch(url,

Java script fetch returns 200 but no data

断了今生、忘了曾经 提交于 2019-12-24 11:37:44
问题 I have a requirement to display all the countries in the world in a drop down. So I found this api end point END POINT LINK. When I copy and paste this end point link in my web browser I got a response with all the data. (countries); When I try to embed this in project. getCountries() { try { fetch(`https://restcountries.eu/rest/v1/all`).then(data => console.log(data) ); } catch (error) { console.log("HERE ERROR COMES", error); } } It does go to then block of the fetch method. But gives me

Why is Fetch API call not returning the response headers?

。_饼干妹妹 提交于 2019-12-24 06:58:41
问题 I'm building a SignUp form and have the following API call to POST the form: const request = new Request('http://localhost:4300/auth/', { method: 'POST', headers: new Headers({ 'Accept' : 'application/json', 'Content-Type' : 'application/json', }), body: JSON.stringify({ user: data }) }); fetch(request).then(response => { console.log(response); const auth = response.headers.get('Authorization'); console.log(auth) }); The problem is response.headers.get('Authorization') is returning as null .

React Native fetch is not a function

扶醉桌前 提交于 2019-12-24 03:07:36
问题 I'm trying to use react native for a simple app. I want to download a json array from a page and display it as a listview. I'm doing this var listApp = React.createClass({ fetchData : function() { fetch(requestURL) .then((response) => response.json()) .then((responseData) => { console.log(responseData); }) .done(); }, componentDidMount: function() { this.fetchData(); this.setState({ dataSource: this.state.dataSource.cloneWithRows(elements) }); }, But doing this I get the error fetch is not a

How to make common API call function using fetch

狂风中的少年 提交于 2019-12-24 00:30:04
问题 I am trying to make common function which will handle all of my API calls from anywhere I am using react": "^16.8.6" and fetch for making api call So far what i have figure out to do is Helper.js export function ApiHelper(url, data = {}, method = 'POST') { let bearer = 'Bearer ' + localStorage.getItem('user_token'); var promise = fetch(url, { method: method, withCredentials: true, // credentials: 'include', headers: { 'Authorization': bearer, 'X-FP-API-KEY': 'chaptoken', 'Content-Type':