How to Resolve a CORS Error in an SPFx React Web Part
Question details
A React SharePoint Framework (SPFx) web part is unable to access an external API due to a Cross-Origin Resource Sharing (CORS) failure.
- Product
- SharePoint Framework (SPFx)
- Device & OS
- not provided
- Scenario
- Developing or running an SPFx React web part that makes data calls to an external backend API.
- Observed behavior
- The preflight (OPTIONS) request fails the access-control check and does not return an HTTP success status, preventing the actual API request from executing.
Verify that you have administrative access to the backend code or server configuration of the external API, as resolving CORS typically requires server-side header changes.
Configure the External API to Handle CORS
Update the external API server configuration to allow requests from your SharePoint domain and return successful preflight responses.
CORS is a browser-based security mechanism that restricts web pages from making requests to a different domain than the one that served the page. To fix this, the destination server must be explicitly configured to accept your SPFx web part's domain via specific HTTP headers.
Open the server-side project for your external API (e.g., ASP.NET Core, Node.js, or Azure Functions).
Implement or enable the CORS middleware appropriate for your backend framework.
Configure the Access-Control-Allow-Origin header to include your specific SharePoint tenant URL (e.g., https://yourtenant.sharepoint.com).
Ensure the server is set up to handle HTTP OPTIONS requests and correctly returns the Access-Control-Allow-Methods and Access-Control-Allow-Headers headers along with an HTTP 200 OK status.
Consult the SharePoint Developer Community
If you do not own the external API or are experiencing complex routing issues, seek guidance from Microsoft's developer resources.
Looking for a Lightweight Office Suite for Your Daily Work?
Developing complex SharePoint web parts requires focus. For your everyday documentation, tracking, and presentation needs, try WPS Office. It is a completely free, lightweight alternative that is highly compatible with Microsoft Office formats.
- 1. Download the installer: Visit the official WPS Office website and download the installation file for your operating system.
- 2. Install WPS Office: Run the installer and follow the simple on-screen instructions to set up the software in minutes.
- 3. Start editing: Open your existing .docx, .xlsx, or .pptx files directly and start working with a familiar, easy-to-use interface.

Frequently Asked Questions
What is a CORS preflight request?
A preflight request is an initial HTTP OPTIONS request sent automatically by the browser before the actual API request. It checks whether the target server understands and permits the CORS protocol for the specific HTTP method and headers you intend to use.
Can I bypass CORS directly within my SPFx React web part code?
No, CORS is a security policy enforced by the web browser, not the client code. You cannot bypass it solely from within your SPFx React code. The target API server must return the appropriate CORS headers, or you must route your request through a backend proxy that handles CORS.
How do I test if my external API is returning the correct CORS headers?
You can open your browser's Developer Tools (F12) and check the Network tab. Look for the failed request marked as OPTIONS. Inspect its response headers to see if Access-Control-Allow-Origin is present and matches your domain. You can also use tools like Postman or cURL to manually send an OPTIONS request to your API endpoint.




