15

I have this action which calls a function:

dispatch(Api({url: "my_url", method: "POST", data: data}))

Here I am passing array as a data..

import fetch from 'isomorphic-fetch'

export default function Api({url, method, headers, data}={}){
    return dispatch => {

        console.log(data)
        console.log(url)
        console.log(method)
        console.log(JSON.stringify(data))
        let response = fetch(url, {
            mode: 'no-cors',
            method: method || null,
            body: data || null, 
        }).then(function(response) {
            console.log("response");
             console.log(response)
            });

    }
}

Here I am using fetch with mode:'no-cors' I guess I am passing all the arguements.. My body here is simple array that I am passing as arguement..

When I see the response it is like :

body: null
bodyUsed: false
headers: Headers
ok: false
status: 0
statusText: ""
type: "opaque"
url:""

Here my body is not being used..

What is wrong in here ? Need help

3
  • Have you got the answer to this ?Iam also facing similar issue...
    – Mothy
    Oct 12, 2016 at 8:26
  • 1
    yes the issue was on server side.. you should allow all origin to fetch
    – varad
    Oct 17, 2016 at 10:17
  • Yes that helped me. I have added <add name="Access-Control-Allow-Origin" value="*" />' in web config file.But i have changed to axios instead of 'isomorphic-fetch'
    – Mothy
    Oct 18, 2016 at 6:35

1 Answer 1

30

You're getting an opaque response [1] [2], because you're using fetch with mode: 'no-cors'. You need to use mode: 'cors' and the server needs to send the required CORS headers [3] in order to access the response.

3
  • I have made to mode:'cors' and made necessary changes as mentioned in asp.net/web-api/overview/security/…. But getting response as (...) bodyUsed:false headers:Headers ok:true status:200 statusText:"OK" type:"cors" url:. Can you please help me to see the data?
    – Mothy
    Oct 12, 2016 at 9:22
  • Do you have a public URL for the service that I can test? Oct 14, 2016 at 10:40
  • Sorry, Currently the api/url is not hosted anywhere.
    – Mothy
    Oct 17, 2016 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.