How to fix CORS error?

Uroš Gavrilović 30 Reputation points
2023-10-16T23:07:27.8366667+00:00

Hi all, pretty new here. This is my first time using Azure. I created static web page where I deployed my React app and created spring app service where I deployed another app, spring boot. Whenever I send a HTTP request from my React app (https://delightful-stone-0033d2303.3.azurestaticapps.net/) to my back-end in Spring Boot (https://fitmaster-server.azuremicroservices.io), I get CORS error. I don't get CORS errors on my localhost. Is this caused by my faulty back-end or do I need to somehow enable it in some Azure options menu? Thanks.

Azure Spring Apps
Azure Spring Apps
An Azure platform as a service for running Spring Boot applications at cloud scale. Previously known as Azure Spring Cloud.
124 questions
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
926 questions
0 comments No comments
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,321 Reputation points
    2023-10-17T15:15:22.7866667+00:00

    Uroš Gavrilović Thanks for posting your question in Microsoft Q&A. CORS is a security feature implemented by web browsers that restricts web pages from making requests to a different domain than the one that served the web page. This is done to prevent malicious websites from accessing sensitive data from other websites. When you try to make a request from your React app to your Spring Boot app, the browser blocks the request because it is coming from a different domain.

    It looks like you haven't configured CORS in your Azure Spring Apps? Check out Configure cross-origin resource sharing doc and configure CORS for origin such as https://delightful-stone-0033d2303.3.azurestaticapps.net/ and needed methods and headers to be allowed in Spring Apps.

    Similarly, you can also configure CORS for Static Web apps so that it can be accessed from different domains (if needed) and refer https://video2.skills-academy.com/en-gb/azure/static-web-apps/configuration#global-headers doc for more info.

    I hope this helps and if you have any questions, let me know. Note: This answer is partially generated based on AI tools.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


1 additional answer

Sort by: Most helpful
  1. Konstantinos Passadis 19,166 Reputation points MVP
    2023-10-18T23:11:10.68+00:00

    Hello @Uroš Gavrilović

    Please try to add this in your code

    app.use(cors({
      origin: 'https://victorious-flower-0f7326e10.3.azurestaticapps.net'
    }));
    
    
    

    If your client-side request includes methods other than GET or headers other than the basics, you need to set Access-Control-Allow-Methods and Access-Control-Allow-Headers on your server.

    I had a similar problem in React and came down to the actual code !

    Also : https://stackoverflow.com/questions/49049312/cors-error-when-connecting-local-react-frontend-to-local-spring-boot-middleware


    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


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.