Wednesday, 4 October 2017

access-control-allow-origin

When do development, especially front end and back end are separated in different projects, it is easy to get  access-control-allow-origin error when call webservices.

For this, we have some solutions:

1) For front end, we can add some proxy logic to redirect front end HTTP request through proxy, for instance, our front end application runs on http://localhost:3000, and back end application on http://localhost:8000, if front end make a service call to back end, say /api/v1/students, the call will proxyed to http://localhost:8000/api/v1/students, not http://localhost:3000/api/v1/students, thus avoiding access-control-allow-origin error from the browser. for instance, if you use gulp serve, in server.js file, you can use 'http-proxy-middleware':

var proxyMiddleware = require('http-proxy-middleware');
server.middleware = proxyMiddleware('/api', {target: 'http://localhost:8888', changeOrigin: true});

For 'http-proxy-middleware' can get more info in: https://github.com/chimurai/http-proxy-middleware

2) Otherwise, we can add some logic on back end, e.g. if we use spring framework,  we can use @CrossOrigin annotation on method, class or global level. For global enabled we can add this annotation in one of the configuration classes is OK:
@Configuration @CrossOrigin public class WebConfig { }

3) Or we can just do something without code, say if we use Chrome, we can add the plugin


This will also avoid this error.

4 comments:

  1. Much obliged to you for sharing this magnificent blog. It will enhance my insight about JAVA systems. This blog will find out about JAVA ideas. Likewise, it will enhance my programming ability.
    Spring Hibernate Training | Spring and Hibernate Training in Chennai | Spring framework Training in Chennai | Spring course in Chennai

    ReplyDelete
  2. Grab Data Science Certification in Chennai for skyrocketing your career with Infycle Technologies, the best Software Training & Placement institutes in and around Chennai. In addition to the Certification, Infycle also gives the best placement training for personality tests, interview preparation, and mock interviews for leveling up the candidate's grades to a professional leveGet Data Science Certification in Chennai | Infycle Technologies

    ReplyDelete
  3. I want to applaud my beautiful wife for finding out about Dr Ozigidon who helped us with the National Lottery Jackpot winning numbers to win a whopping sum of £184 Million in the Euro-million win. My name is Joe Thwaite, 49, and my wife is Jess Thwaite, 46, from Gloucestershire. On May 10, 2022 we became the biggest national lottery jackpot winner after we had contacted Dr Ozigidon to help us become a large winner in the lottery with his powerful spell because my wife and I have read a lot about his powerful and effective spell. He gave us the winning numbers after casting the spell and assured us of victory. We followed and obeyed Dr Ozigidon spell instructions and today we are celebrating after winning the record-breaking Euro-Millions jackpot of £184M from the draw on Tuesday 10 May, 2022. Thanks to Dr Ozigidon because his good work has really been wonderful with his magical spells. We usually play the lottery but coming up with a big win has never been our thing not until we came in contact with Dr Ozigidon who a lot of people have been so thankful to for his miraculous doings. We are so grateful to this power sorcerer Dr Ozigidon. For giving us victory with his powerful spell. We decided to go public to motivate others and also to tell everyone about Dr Ozigidon powerful spell. Contact him today if you wish. His email: drozigidonhenz.spell.net@gmail.com ]. We need extraordinary confidence to make things happen magically in our lives.

    ReplyDelete