fetch-api

How do I copy a Request object with a different URL?

怎甘沉沦 提交于 2019-12-05 00:45:14
I'm writing a wrapper around fetch that I would like to add something to the URL before making the request e.g. identifying query parameters. I can't figure out how to make a copy of a given a Request object with a different URL than the original. My code looks like: // My function which tries to modify the URL of the request function addLangParameter(request) { const newUrl = request.url + "?lang=" + lang; return new Request(newUrl, /* not sure what to put here */); } // My fetch wrapper function myFetch(input, init) { // Normalize the input into a Request object return Promise.resolve(new

What's the difference between “same-origin” and “no-cors” for JavaScript's Fetch API?

折月煮酒 提交于 2019-12-05 00:15:26
I thought same origin implies no CORS, and vice-versa. What's the difference between the two options for JavaScript's Fetch API's mode option? Also, in the specs, it says: Even though the default request mode is "no-cors", standards are highly discouraged from using it for new features. It is rather unsafe. Why is it unsafe? Source: https://fetch.spec.whatwg.org/#requests With same-origin you can perform requests only to your origin, otherwise the request will result in an error. With no-cors , you can perform requests to other origins, even if they don't set the required CORS headers, but you

How do I fix CORS issue in Fetch API

老子叫甜甜 提交于 2019-12-04 23:47:03
问题 I'm building a front-end only basic Weather App using reactjs. For API requests I'm using Fetch API. In my app, I'm getting the current location from a simple API I found and it gives the location as a JSON object. But when I request it through Fetch API, I'm getting this error. Failed to load http://ip-api.com/json: Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response. So I searched through and found multiple solutions to fix

Fetch API not getting full HTML page

南笙酒味 提交于 2019-12-04 22:52:29
I'm using the Fetch API to get some (public) data from a webpage. Here is my code: const proxyurl = "https://cors-anywhere.herokuapp.com/"; var bookUrl = "the+old+man+and+the+sea"; var url = 'https://miss.ent.sirsidynix.net/client/en_US/mlsathome/search/results?qu=' + bookUrl + '&te=ILS'; fetch(proxyurl + url).then(function(result){ return result.text(); }).then(function(text){ var parser = new DOMParser(); var html = parser.parseFromString(text, 'text/html'); var divs = html.getElementsByTagName('div'); var x = html.getElementsByTagName('thead'); console.log(divs); console.log(x); }); The

Fetch GET Request with custom headers ReactJS

心不动则不痛 提交于 2019-12-04 21:31:15
问题 I am trying to send a GET request to a API but when i add custom headers in the code somthing strange happens. Somewhere the request method changes to OPTIONS when it reaches the web server. But when i do the same without headers it will be a GET type. When i use the application postman (API development tool) the request works fine! request code: let token = this.generateClientToken(privateKey, message); let myheaders = { "appID": appID, "authorizationkey": token } fetch('http://localhost

React cannot read property map of undefined

纵饮孤独 提交于 2019-12-04 20:48:42
问题 I am very new to react and I am trying to bring in data from a rails api but I am getting the error TypeError: Cannot read property 'map' of undefined If i use the react dev tools I can see the state and I can see the contacts if I mess around with it in the console using $r.state.contacts Can someone help with what I have done wrong? my component looks like this: import React from 'react'; import Contact from './Contact'; class ContactsList extends React.Component { constructor(props) {

Using Fetch API with Promise.all

可紊 提交于 2019-12-04 16:53:20
my aim is to fetch data from two URLs and perform an action only when both have come back successfully. On the other hand i want to return an error if either of them fail. I have played around with my code and managed to get the desired effect. My question is, is there a more efficient, succinct way of achieving the same functionality? Helper functions let status = (r) => { if (r.ok) { return Promise.resolve(r) } else { return Promise.reject(new Error(r.statusText)) } } let json = (r) => r.json(); Requests let urls = [ 'http://localhost:3000/incomplete', 'http://localhost:3000/complete' ] let

How to check if the response of a fetch is a json object in javascript

不羁的心 提交于 2019-12-04 07:53:08
问题 I'm using fetch polyfill to retrieve a JSON or text from a URL, I want to know how can I check if the response is a JSON object or is it only text fetch(URL, options).then(response => { // how to check if response has a body of type json? if (response.isJson()) return response.json(); }); 回答1: You could check for the content-type of the response, as shown in this MDN example: fetch(myRequest).then(response => { const contentType = response.headers.get("content-type"); if (contentType &&

using of fetch API the unexpected end of input error occurr

痴心易碎 提交于 2019-12-04 05:50:06
问题 fetch('http://www.freegamesforyourwebsite.com/feeds/games/? tag=platform&limit=100&format=json', { method:'GET', mode:'no-cors', dataType: 'json', headers: { 'Accept': 'application/json' } }).then(response => response.json()).then(addImage).catch(e => requestError(e,'image')); console.log SyntaxError: Unexpected end of input at fetch.then.response ((index):159) at <anonymous> I try using the fetch the json with cros platform, why the request header in Google developer tool shows me the

Request with URL that includes credentials

為{幸葍}努か 提交于 2019-12-04 05:23:56
I'm trying to fetch a curl and get a JSON from an API. curl -XPOST -d "grant_type=password" -d "username=admin@admin.admin" \ -d "password=admin" "web_app@localhost:8081/oauth/token" When I use the curl in terminal everything works fine but trying it with a fetch I get the error message mentioned at the bottom. fetch("http://web_app@localhost:8081/oauth/token", { credentials: 'include', body: "grant_type=password&username=admin@admin.admin&password=admin", headers: { "Content-Type": "application/x-www-form-urlencoded", }, method: "POST" } This is the error I get: TypeError: http://web_app