fetch-api

Setting query string using Fetch GET request

笑着哭i 提交于 2019-11-26 06:27:49
问题 I\'m trying to use the new Fetch API: https://developer.mozilla.org/en/docs/Web/API/Fetch_API I am making a GET request like this: var request = new Request({ url: \'http://myapi.com/orders\', method: \'GET\' }); fetch(request); However, I\'m unsure how to add a query string to the GET request. Ideally I want to be able to make a GET request to a URL like: \'http://myapi.com/orders?order_id=1\' In jQuery I could do this by passing {order_id: 1} as the data parameter of $.ajax() . Is there an

What is an opaque response, and what purpose does it serve?

大城市里の小女人 提交于 2019-11-26 06:17:27
问题 I tried to fetch the URL of an old website, and an error happened: Fetch API cannot load http://xyz. No \'Access-Control-Allow-Origin\' header is present on the requested resource. Origin \'http://abc\' is therefore not allowed access. If an opaque response serves your needs, set the request\'s mode to \'no-cors\' to fetch the resource with CORS disabled. I understood the message, and tried to do a request that returns an opaque response: fetch(\"http://xyz\", {\'mode\': \'no-cors\'}) Ok, it

How can I fetch an array of URLs with Promise.all?

感情迁移 提交于 2019-11-26 06:06:24
问题 If I have an array of urls: var urls = [\'1.txt\', \'2.txt\', \'3.txt\']; // these text files contain \"one\", \"two\", \"three\", respectively. And I want to build an object that looks like this: var text = [\'one\', \'two\', \'three\']; I’ve been trying to learn to do this with fetch , which of course returns Promise s. Some things I’ve tried that don’t work: var promises = urls.map(url => fetch(url)); var texts = []; Promise.all(promises) .then(results => { results.forEach(result => result

Post a x-www-form-urlencoded request from React Native

 ̄綄美尐妖づ 提交于 2019-11-26 05:27:37
问题 I have some parameters that I want to POST form-encoded to my server: { \'userName\': \'test@gmail.com\', \'password\': \'Password!\', \'grant_type\': \'password\' } I\'m sending my request (currently without parameters) like this var obj = { method: \'POST\', headers: { \'Content-Type\': \'application/x-www-form-urlencoded; charset=UTF-8\', }, }; fetch(\'https://example.com/login\', obj) .then(function(res) { // Do stuff with result }); How can I include the form-encoded parameters in the

Upload progress indicators for fetch?

淺唱寂寞╮ 提交于 2019-11-26 05:24:14
问题 I\'m struggling to find documentation or examples of implementing an upload progress indicator using fetch. This is the only reference I\'ve found so far, which states: Progress events are a high level feature that won\'t arrive in fetch for now. You can create your own by looking at the Content-Length header and using a pass-through stream to monitor the bytes received. This means you can explicitly handle responses without a Content-Length differently. And of course, even if Content-Length

Fetch API with Cookie

不问归期 提交于 2019-11-26 05:15:59
问题 I am trying out the new Fetch API but is having trouble with Cookies. Specifically, after a successful login, there is a Cookie header in future requests, but Fetch seems to ignore that headers, and all my requests made with Fetch is unauthorized. Is it because Fetch is still not ready or Fetch does not work with Cookies? I build my app with Webpack. I also use Fetch in React Native, which does not have the same issue. 回答1: Fetch does not use cookie by default. To enable cookie, do this:

fetch: Reject promise with JSON error object

痞子三分冷 提交于 2019-11-26 03:51:49
问题 I have an HTTP API that returns JSON data both on success and on failure. An example failure would look like this: ~ ◆ http get http://localhost:5000/api/isbn/2266202022 HTTP/1.1 400 BAD REQUEST Content-Length: 171 Content-Type: application/json Server: TornadoServer/4.0 { \"message\": \"There was an issue with at least some of the supplied values.\", \"payload\": { \"isbn\": \"Could not find match for ISBN.\" }, \"type\": \"validation\" } What I want to achieve in my JavaScript code is

How do I upload a file with the JS fetch API?

霸气de小男生 提交于 2019-11-26 03:04:36
问题 I am still trying to wrap my head around it. I can have the user select the file (or even multiple) with the file input: <form> <div> <label>Select file to upload</label> <input type=\"file\"> </div> <button type=\"submit\">Convert</button> </form> And I can catch the submit event using <fill in your event handler here> . But once I do, how do I send the file using fetch ? fetch(\'/files\', { method: \'post\', // what goes here? What is the \"body\" for this? content-type header? }).then(/*

How do I post form data with fetch api?

穿精又带淫゛_ 提交于 2019-11-26 02:35:40
问题 My code: fetch(\"api/xxx\", { body: new FormData(document.getElementById(\"form\")), headers: { \"Content-Type\": \"application/x-www-form-urlencoded\", // \"Content-Type\": \"multipart/form-data\", }, method: \"post\", } I tried to post my form using fetch api, and the body it sends is like: -----------------------------114782935826962 Content-Disposition: form-data; name=\"email\" test@example.com -----------------------------114782935826962 Content-Disposition: form-data; name=\"password\"

5xx or 4xx error with “No &#39;Access-Control-Allow-Origin&#39; header is present”

喜你入骨 提交于 2019-11-26 02:25:38
问题 My browser is logging the following message in the devtools console: No \'Access-Control-Allow-Origin\' header is present on the requested resource.… The response had HTTP status code 503. Background : I have two apps. One that is an Express Node application connected to a Mongo database. The other is a basic web application that makes POST requests to the Node application via the Fetch API to get data from Mongo. Issue : Though I receive no CORS errors on my local machine, I am given the