fetch-api

Fetch Api unable to get Session from PHP server

╄→尐↘猪︶ㄣ 提交于 2019-12-01 17:35:31
I am using Fetch Api in my application. I've got a PHP server page to get session data which was already defined before. It seemd like this: <?php header('Content-Type: application/json; charset=UTF-8'); header('Access-Control-Allow-Origin: *'); session_start(); // $_SESSION['data'] already defined before $result = array(); // print_r($_SESSION['data']); if (isset($_SESSION['data'])) { $result = $_SESSION['data']; $result['code'] = 'ok'; } else { $result['code'] = 'error'; } echo json_encode($result, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); I also got another html page to get the

Fetch Api unable to get Session from PHP server

≯℡__Kan透↙ 提交于 2019-12-01 16:33:55
问题 I am using Fetch Api in my application. I've got a PHP server page to get session data which was already defined before. It seemd like this: <?php header('Content-Type: application/json; charset=UTF-8'); header('Access-Control-Allow-Origin: *'); session_start(); // $_SESSION['data'] already defined before $result = array(); // print_r($_SESSION['data']); if (isset($_SESSION['data'])) { $result = $_SESSION['data']; $result['code'] = 'ok'; } else { $result['code'] = 'error'; } echo json_encode(

Cannot fetch data from Wikipedia API

眉间皱痕 提交于 2019-12-01 13:08:11
let dataObj = []; const query = 'marvel'; fetch(`https://en.wikipedia.org/w/api.php?action=query&titles=${query}&prop=revisions&rvprop=content&format=json&formatversion=2`) .then(data => data.json()) .then(data => dataObj.push(data)) .catch(err => console.log(err)); This is the error that I receive: No ' Access-Control-Allow-Origin ' header is present on the requested resource. Origin ' https://s.codepen.io ' 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 set the mode to no-cors but

How to include both a parsed response and original response headers in fetch errors [duplicate]

梦想与她 提交于 2019-12-01 10:52:46
This question already has an answer here: How do I access previous promise results in a .then() chain? 17 answers I have the following promise chain: return fetch(request) .then(checkStatus) .then(response => response.json()) .then(json => ({ response: json })) .catch(error => ({ error })) Where checkstatus() checks if the request was successful, and returns an error if it wasn't. This error will be caught and returned. But, the problem is that I want to add the both response.statusText and the results of response.json() to the error. The problem is that when I parse it I lose the original

Response header not showing

心不动则不痛 提交于 2019-12-01 10:38:14
I am building a simple web app using ReactJS and create-react-app . I have a backend API set up on Heroku where I can make POST requests. Everything works fine, except: When I make a POST request using fetch API, the response is 100% correct but it only gives me 2 standard headers. I want to get my custom header. I have added expose header in my response and here's the plot twist: When I view the headers from Chrome Inspection Tool or Postman (API tool), it shows all the headers, including my custom one. Here is the fetch code I'm using - fetch(theUrl, { method: 'POST', body: JSON.stringify({

Read chunked binary response with Fetch API

柔情痞子 提交于 2019-12-01 04:51:56
How do I read a binary chunked response using the Fetch API. I'm using the following code which works insofar it reads the chunked response from the server. However, the data seems to be encoded/decoded somehow causing the getFloat32 to sometimes fail. I've tried to read the response with curl, and that works fine, leading me to believe I need to do something to make fetch api treat chunks as binary. The content-type of the response is properly set to "application/octet-stream". const consume = responseReader => { return responseReader.read().then(result => { if (result.done) { return; } const

Read chunked binary response with Fetch API

安稳与你 提交于 2019-12-01 02:09:44
问题 How do I read a binary chunked response using the Fetch API. I'm using the following code which works insofar it reads the chunked response from the server. However, the data seems to be encoded/decoded somehow causing the getFloat32 to sometimes fail. I've tried to read the response with curl, and that works fine, leading me to believe I need to do something to make fetch api treat chunks as binary. The content-type of the response is properly set to "application/octet-stream". const consume

React-Native Populate image with URL that has been converted from a blob

↘锁芯ラ 提交于 2019-12-01 01:39:05
I am trying to populate an image with a URl <Image source={{uri: this.state.imageURL}} style={styles.thumb} /> I request the image from the server, using the fetch API, and it returns a blob. I then convert the BLOB into a URL using the following line: var imageURL = window.URL.createObjectURL(attachmentBLOB); When I console.log() the imageURL it prints: blob:http%3A//localhost%3A8081/4ce24d92-0b7e-4350-9a18-83b74bed6f87 I am getting no errors or warning. The image is just not displaying I am using the android emulator. Please help. Thanks in advance! Solution React-Native does not support

Relative paths with fetch in Javascript

為{幸葍}努か 提交于 2019-11-30 17:13:58
I was surprised by an experience with relative paths in Javascript today. I’ve boiled down the situation to the following: Suppose you have a directory structure like: app/ | +--app.html +--js/ | +--app.js +--data.json All my app.html does is run js/app.js <!DOCTYPE html> <title>app.html</title> <body> <script src=js/app.js></script> </body> app.js loads the JSON file and sticks it at the beginning of body : // js/app.js fetch('js/data.json') // <-- this path surprises me .then(response => response.json()) .then(data => app.data = data) The data is valid JSON, just a string: "Hello World" This

Fetch API - What's the use of redirect: manual

白昼怎懂夜的黑 提交于 2019-11-30 17:02:29
I've recently been playing with the Javascript Fetch API. As far as I understand, by default all redirects are handled transparently and in the end I get a response from the last call in the redirect chain. However, I could invoke fetch with {redirect: 'manual'}, in which case it would return an opaqueredirect response with no usable information. From https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect", status is 0, status message is the empty byte sequence, header list is empty,