Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set {mode:'no-cors'} in fetch #151

Closed
vuhaininh opened this issue Jun 25, 2017 · 9 comments
Closed

Set {mode:'no-cors'} in fetch #151

vuhaininh opened this issue Jun 25, 2017 · 9 comments

Comments

@vuhaininh
Copy link

Hi, how can I set {mode:'no-cors'} in fetch because I have problem with CORS

"FETCH API cannot load
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' 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."

Thanks

@lexich
Copy link
Owner

lexich commented Jun 25, 2017

@vuhaininh
Copy link
Author

vuhaininh commented Jun 25, 2017

Hi,
I set: options: {
mode: 'no-cors'
}
the fetch is completed but I do not get data in actions / reducers
Could you please fetch Data from this:

`${YLE_URL}items.json?q=finland&mediaobject=${MEDIA_TYPE}&app_id=${APP_ID}&app_key=${APP_KEY}`

const YLE_URL = `https://external.api.yle.fi/v1/programs/`;
const APP_ID = '7d027e13';
const APP_KEY = '88e86f19ddfcb0ee4f0a996fc74d8658';
const MEDIA_TYPE = 'audio';

My code is:

export default reduxApi({
    audios: {
      url: `${YLE_URL}items.json?q=:term&mediaobject=${MEDIA_TYPE}&app_id=${APP_ID}&app_key=${APP_KEY}`,

      options: {
        mode: 'no-cors'
      }
    }

}).use("fetch",adapterFetch(fetch));

@lexich
Copy link
Owner

lexich commented Jun 25, 2017

When I do the same in console fetch(url, { mode: 'no-cors' }) I have error:
Refused to connect to 'https://external.api.yle.fi/v1/programs/items.json?q=finland&mediaobject=audio&app_id=7d027e13&app_key=88e86f19ddfcb0ee4f0a996fc74d8658' because it violates the document's Content Security Policy.
Redux api is only lightweight wrapper above fetch api and redux. Pls write you workable code with fetch and i help you implement it. But you example above looks correct

@vuhaininh
Copy link
Author

vuhaininh commented Jun 25, 2017

Hi I run the following code in console and it works. Could you please check again.
fetch("https://external.api.yle.fi/v1/programs/items.json?q=finland&mediaobject=audio&app_id=7d027e13&app_key=88e86f19ddfcb0ee4f0a996fc74d8658",{mode: 'no-cors'})

When I used options mode no-cors in redux-api, it can fetch but it does not return data to state.audios ( null )

@vuhaininh
Copy link
Author

I open a new blank tab in Chrome to run the code
fetch("https://external.api.yle.fi/v1/programs/items.json?q=finland&mediaobject=audio&app_id=7d027e13&app_key=88e86f19ddfcb0ee4f0a996fc74d8658",{mode: 'no-cors'})

@lexich
Copy link
Owner

lexich commented Jun 25, 2017

Try this. fetch("https://external.api.yle.fi/v1/programs/items.json?q=finland&mediaobject=audio&app_id=7d027e13&app_key=88e86f19ddfcb0ee4f0a996fc74d8658",{mode: 'no-cors'}).then(d => d.text()).then(d => console.log("Done", d)).catch(err => console.error("Error", err)) Response body is empty.

@vuhaininh
Copy link
Author

Ok I see that.
So I am confusing how I can do to get data from that api

@kolyasya
Copy link

kolyasya commented Oct 6, 2017

@lexich I have the same problem. If I have no { mode: 'no-cors' } setting and use Chrome CORS Extension I have my data with no problems.
When I switch { mode: 'no-cors' } and disable CORS Extension I have a successful request and it seems like it receives the data, but I have nothing in my Redux Store.

image

image

I tested fetch in console with disabled CORS extension and enabled mode: 'no-cors'
image

@lexich
Copy link
Owner

lexich commented Oct 6, 2017

Hi @kolyasya There is no any content in your response. image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants