[英]Can't access backend API using CORS when making a AJAX call using React frontend
I am trying create an application that grabs external API information through the backend and passes it to my React frontend. 我正在尝试创建一个通过后端获取外部API信息的应用程序,并将其传递给我的React前端。 The Backend works fine, i can make calls to the server and i get the information. 后端工作正常,我可以调用服务器,并且可以获取信息。 However when i try to pass the Information to my front end i get errors. 但是,当我尝试将信息传递给我的前端时,会出现错误。 If someone could tell me what i did wrong that would be greatly appreciated. 如果有人可以告诉我我做错了什么,将不胜感激。 i've been having this issue all weekend. 我整个周末都在遇到这个问题。
backend controller: 后端控制器:
url: 网址:
However when i make an AJAX call through the react front end i get this error: 但是,当我通过react前端进行AJAX调用时,出现此错误:
Backend error: 后端错误:
Here is the rest of my code: 这是我其余的代码:
Rails: CORS and Application controller: Rails:CORS和应用程序控制器:
Rails Routes: Rails路线:
React Component w/ AJAX call: 使用AJAX调用的React Component:
This is CORS
error. 这是CORS
错误。 Your server should respond on OPTIONS
request with some data which will satisfy browser when you performing call from one domain (where your frontend hosted) to another domain (where your server at). 当您执行从一个域(前端托管的位置)到另一个域(服务器所在的位置)的呼叫时,您的服务器应响应OPTIONS
请求并提供一些数据,这些数据将满足浏览器的要求。
Check MDN for more info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS 检查MDN以获取更多信息: https : //developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.