How to fix CORS issue when accessing an API from a React app on localhost

Gokul R Dev 351 Reputation points
2024-09-25T06:07:38.0566667+00:00

I am trying to access an API from my React app running on localhost, but I'm getting a CORS error: "Access to fetch at '<api url>' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource". I have tried adding CORS policies for all operations, the specific API, and the product under which this API is published, but I'm still getting the error. Can anyone help me resolve this issue? The policy is this :

<!--
    - Policies are applied in the order they appear.
    - Position <base/> inside a section to inherit policies from the outer scope.
    - Comments within policies are not preserved.
-->
<!-- Add policies as children to the <inbound>, <outbound>, <backend>, and <on-error> elements -->
<policies>
    <!-- Throttle, authorize, validate, cache, or transform the requests -->
    <inbound>
        <base />
        <cors allow-credentials="false">
            <allowed-origins>
                <origin>http://localhost:5173</origin>
            </allowed-origins>
            <allowed-methods>
                <method>GET</method>
                <method>POST</method>
            </allowed-methods>
        </cors>
    </inbound>
    <!-- Control if and how the requests are forwarded to services  -->
    <backend>
        <base />
    </backend>
    <!-- Customize the responses -->
    <outbound>
        <base />
    </outbound>
    <!-- Handle exceptions and customize error responses  -->
    <on-error>
        <base />
    </on-error>
</policies>

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Gokul R Dev 351 Reputation points
    2024-09-25T08:31:14.19+00:00

    Issue is solved , had another CORS policy scoped for all apis, edited it to allow the orgin.

    1 person found this answer helpful.
    0 comments No comments

  2. LeelaRajeshSayana-MSFT 17,766 Reputation points Moderator
    2024-09-25T14:45:00.66+00:00

    Hi @Gokul R Dev I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer .

    Error Message:

    "Access to fetch at '<api url>' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource"

    Issue:

    Trying to fetch an API from React application is resulting in CORS error even after making sure that CORS has been enabled on the API

    Solution:

    There was a different CORS policy applied on all the APIs which caused this error. Modifying it resoled the issue.

    If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information.

    I hope this helps!

    If you have any other questions, please let me know. Thank you again for your time and patience throughout this issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.