Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text response is empty when using fetch

The following code:

fetch('http://localhost:8080/root/1487171054127/k_query_bearer_token', {
        mode: 'no-cors', credentials: 'include'
    })
        .then(function (response) {
            return response.text();
        })
        .then(function (text) {
            console.log('Request successful', text.length);
        })
        .catch(function (error) {
            log('Request failed', error)
        });

is outputting:

Request successful 0

If I use curl:

curl 'http://localhost:8080/root/1487171054127/k_query_bearer_token' \
  -H 'Cookie: JSESSIONID=CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5' 

I get a token in text form back (length != 0).

And if I output the response header via:

curl 'http://localhost:8080/root/1487171054127/k_query_bearer_token' 
  -H 'Cookie: JSESSIONID=CviS9IK8pcsqADdP-m0MRXX_AvUqfzjJPwk1Yytf.ee16d0a01ad5'
  --head

I get:

HTTP/1.1 200 OK
Connection: keep-alive
X-Powered-By: Undertow/1
Server: JBoss-EAP/7
Content-Type: text/plain
Content-Length: 1730
Date: Wed, 15 Feb 2017 16:17:00 GMT

Why am I getting no text via fetch?

like image 947
Baz Avatar asked Feb 15 '17 16:02

Baz


People also ask

How do I get fetch to respond to a text?

Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.

How do I fix fetch error in CORS?

To solve the "TypeError: Failed to fetch", make sure to pass the correct configuration to the fetch method, including the URL, HTTP method and headers, and verify that the server you're making a request to is setting the correct CORS headers with the response. Copied!

How do you get HTML response in Fetch?

To get a response as an HTML string, you can use the text() method. Here is an example that downloads the Google homepage as an HTML string and prints it on the console: fetch('https://www.google.com') . then(res => res.

What does Response text () do?

text() The text() method of the Response interface takes a Response stream and reads it to completion. It returns a promise that resolves with a String .


1 Answers

Remove mode: 'no-cors'.

When you use no-cors mode, you’re explicitly specifying that you want an “opaque response”.

Your script can’t access any properties of an opaque response—instead essentially all you can do is cache it. no-cors mode is basically useful only when doing caching with Service Workers.

If the reason you have your script using no-cors mode is because cross-origin requests to the server otherwise won’t work, the right solution is either to update the server-side code to send the Access-Control-Allow-Origin response header and other CORS headers—if you have access to the server do to that—or else, use a proxy like https://cors-anywhere.herokuapp.com/.

like image 109
sideshowbarker Avatar answered Oct 17 '22 05:10

sideshowbarker