Sat
Sat

Reputation: 21

CORS issue in React js

I am using React js in front end and webApi (c#) for service side. Initially i got issue with CORS while trying to get data from db and then i have added the below lines in web.config file then the issue got resolved. I am able to get data from web api.

<customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type,Origin,Accept" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>

But now i am trying to post data, that is sending data from react js to web api, it is throwing the same cors error. please find the below lines from react js

fetch(url, {
    method: 'POST',
    mode:'cors',
    // body: JSON.stringify(data),
    // headers :{
    //   'Content-Type': 'application/json',
    //   'Access-Control-Allow-Origin':'*'
    // },
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin':'*',
      'Access-Control-Allow-Headers':'Origin,Content-Type,Accept',

    }
  });

Is there anything i am missing. I dont understand why it is working for getting data and not for posting data. Any one can help me on this? Thanks in advance.

Upvotes: 0

Views: 821

Answers (2)

Sat
Sat

Reputation: 21

i have installed Microsoft.AspNet.WebApi.Cors dll and the cors issue got resolved. Thanks all.

Upvotes: 1

Sabesan
Sabesan

Reputation: 702

I faced the smiler issue when I fetch the data from medium.com. In there the CROS issue happened by RSS feed. if you fetch the RES feed data from backend then use RSStoJSON api. To get https://rss2json.com/#rss_url=http%3A%2F%2Ffeeds.twit.tv%2Fbrickhouse.xml

To post use this dependency in your project https://www.npmjs.com/package/jsonfeed-to-rss

But I followed those mechanisms

I couldn’t exactly say this problem is same as your problem. If you mentioned that error it’s easy to answer for me

Upvotes: 0

Related Questions