CORS Error When Accessing External API in React SPFx Web Part

Pravesh Kumar 0 Reputation points
2024-10-23T05:04:04.3533333+00:00

I am trying to access an external API (https://external/admin/api/get_users) from my React SPFx web part, but I encounter the following CORS error:

"Access to fetch at 'https://external/api/get_users' from origin 'https://mysite.sharepoint.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status."

What steps can I take to resolve this issue? Are there specific configurations I need to adjust in my SPFx web part or on the API side to allow this request?

Microsoft 365 and Office SharePoint Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2024-10-23T07:25:20.7966667+00:00

    Hi @Pravesh Kumar,

    The supported way to authenticate SharePoint framework components to a custom API is by using Azure Active Directory (AAD) and OAuth.

    You need to AAD-protect your API. You can configure it so it supports two authentication mechanisms: AAD and your current authentication method. For example, if a JWT token is present, you use AAD+OAuth, and if not you use your other authentication method.

    The SPFx to API authentication mechanism is described in details in the page Connect to Azure AD-secured APIs in SharePoint Framework solutions.

    In summary, you will need the following elements:


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.