0

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.

5
  • Please try to use Axios library instead of fetch. For example, axios.get('some url') Commented Dec 3, 2019 at 18:39
  • What's your CORS error ? Commented Dec 3, 2019 at 18:50
  • @TopW3 Thank you for the solution. i ll try it but my question is why it is working for get data from api not for posting data.? Commented Dec 3, 2019 at 18:57
  • I guess it is the problem of back-end. Please refer this page: expressjs.com/en/resources/middleware/cors.html Commented Dec 3, 2019 at 19:02
  • @Arkerone Failed to load resource: the server responded with a status of 405 (Method Not Allowed) Access to fetch at 'the url' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. asyncToGenerator.js:6 Uncaught (in promise) TypeError: Failed to fetch Commented Dec 3, 2019 at 19:04

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Error message: Failed to load resource: the server responded with a status of 405 (Method Not Allowed) Access to fetch at 'the url' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. asyncToGenerator.js:6 Uncaught (in promise) TypeError: Failed to fetch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.