API Integration Cross Origin Issue in React JS.

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I am creating a PWA(Progressive Web Application) using https://forum.tuteehub.com/tag/react">react js.I am calling a login api from http://expgnx.com/api/v1 on http://localhost:3000.It is throwing the following error-

 

" No 'Access-Control-Allow-Origin' header is https://forum.tuteehub.com/tag/present">present on requested resource. Origin 'http://localhost:3000' is therefore not allowed access.".

 

Please help me out it stuck to me.

profilepic.png
manpreet 2 years ago

You can't access api as public on different origin, port or protocol.The api you are accessing">accessing using having secure protocol, different origin 'expqnx.com' and you are accessing">accessing it on 'http://localhost:3000' using different protocol and origin so as per browser security rules policy you can't do so.

To resolve this issue

 

1)Go to the source where your api is running

2)open api code and 

3)mention the following headers in your api:

  if your api in node:

res.header ("Access-Control-Allow-origin", "*");

res.header(

"Access-Control-Allow-Headers",

"Origin, X-Requested-With, Content-Type, Accept, Authorization"

 

The above i am giving the code snipped for api written in node.

You can set 'Access-Control-Allow-Origin' and 'Access-Control-Allow-Headers':'Origin' for your api written in any language.

 


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community