fetch-api

What limitations apply to opaque responses?

别来无恙 提交于 2019-11-26 01:58:55
问题 Opaque responses are defined as part of the Fetch API, and represent the result of a request made to a remote origin when CORS is not enabled. What practical limitations and \"gotchas\" exist around how opaque responses can be used, both from JavaScript and as resources on a page? 回答1: Access to Opaque Responses' Headers / Body The most straightforward limitation around opaque responses is that you cannot get meaningful information back from most of the properties of the Response class, like

What does it mean when an HTTP request returns status code 0?

早过忘川 提交于 2019-11-26 01:39:37
问题 What does it mean when JavaScript network calls such as fetch or XMLHttpRequest, or any other type of HTTP network request, fail with an HTTP status code of 0? This doesn\'t seem to be a valid HTTP status code as other codes are three digits in HTTP specification. I tried unplugging the network completely as a test. It may be unrelated, but that resulted in status code 17003 (IIRC), which cursory searching suggests means \"DNS server lookup failed\". The same code works fine from some

How do I cancel an HTTP fetch() request?

风格不统一 提交于 2019-11-26 01:07:12
问题 There is a new API for making requests from JavaScript: fetch(). Is there any built in mechanism for canceling these requests in-flight? 回答1: TL/DR: fetch now supports a signal parameter as of 20 September 2017, but not all browsers seem support this atm . This is a change we will be seeing very soon though, and so you should be able to cancel a request by using an AbortController s AbortSignal . Long Version How to: The way it works is this: Step 1 : You create an AbortController (For now I

Trying to use fetch and pass in mode: no-cors

岁酱吖の 提交于 2019-11-25 23:39:39
问题 I can hit this endpoint, http://catfacts-api.appspot.com/api/facts?number=99 via Postman and it returns JSON Additionally I am using create-react-app and would like to avoid setting up any server config. In my client code I am trying to use fetch to do the same thing, but I get the error: No \'Access-Control-Allow-Origin\' header is present on the requested resource. Origin \'http://localhost:3000\' is therefore not allowed access. If an opaque response serves your needs, set the request\'s

Why does .json() return a promise?

匆匆过客 提交于 2019-11-25 23:38:06
问题 I\'ve been messing around with the fetch() api recently, and noticed something which was a bit quirky. let url = \"http://jsonplaceholder.typicode.com/posts/6\"; let iterator = fetch(url); iterator .then(response => { return { data: response.json(), status: response.status } }) .then(post => document.write(post.data)); ; post.data returns a Promise object. http://jsbin.com/wofulo/2/edit?js,output However if it is written as: let url = \"http://jsonplaceholder.typicode.com/posts/6\"; let

Handle response - SyntaxError: Unexpected end of input when using mode: 'no-cors'

别来无恙 提交于 2019-11-25 23:37:59
问题 I tried a ReactJS fetch call to a REST-API and want to handle the response. The call works, i get a response, which i can see in Chrome Dev Tools: function getAllCourses() { fetch(\'http://localhost:8080/course\', { method: \'POST\', mode: \'no-cors\', credentials: \'same-origin\', headers: { \'Accept\': \'application/json\', \'Content-Type\': \'application/json\', }, body: JSON.stringify({ objectClass: \'course\', crud: \'2\' }) }).then(function (response) { console.log(response); return

Fetch: POST json data

佐手、 提交于 2019-11-25 22:35:12
问题 I\'m trying to POST a JSON object using fetch. From what I can understand, I need to attach a stringified object to the body of the request, e.g.: fetch(\"/echo/json/\", { headers: { \'Accept\': \'application/json\', \'Content-Type\': \'application/json\' }, method: \"POST\", body: JSON.stringify({a: 1, b: 2}) }) .then(function(res){ console.log(res) }) .catch(function(res){ console.log(res) }) When using jsfiddle\'s json echo I\'d expect to see the object I\'ve sent ( {a: 1, b: 2} ) back,

No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API

我的未来我决定 提交于 2019-11-25 21:37:34
问题 I\'m trying to fetch some data from the REST API of HP Alm. It works pretty well with a small curl script - I get my data. Now doing that with JavaScript, fetch and ES6 (more or less) seems to be a bigger issue. I keep getting this error message: Fetch API cannot load . Response to preflight request doesn\'t pass access control check: No \'Access-Control-Allow-Origin\' header is present on the requested resource. Origin \'http://127.0.0.1:3000\' is therefore not allowed access. The response