简体   繁体   English

使用React前端进行AJAX调用时,无法使用CORS访问后端API

[英]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.

相关问题 将 React 用作前端时,如何在 Ruby API 中访问用户的 Google 日历? - How do I get access User's Google Calendar in my Ruby API when using it with React as a Frontend? 由于Rails / AJAX应用程序上的CORS,无法在本地访问IBM Watson API - Can't access IBM Watson API locally due to CORS on a Rails/AJAX App 将前端POST / PUTS反馈到Rails 5 API后端 - React frontend POST / PUTS to Rails 5 API backend 将应用程序部署到Heroku时,对Rails API后端和React前端的身份验证请求失败 - Authenticated requests for an Rails API backend and React frontend fail when app deployed to Heroku 使用部分前端时处理来自 rails api 的 401 未经授权错误(反应) - Handling the 401 Unauthorized error from rails api when using a partial frontend (react) Rails前端,后端和API - Rails Frontend, Backend, and API 有哪些方法可以使用 Reactjs 作为前端和 rails api 后端为 webapp 添加身份验证? - What are some ways to add authentication for webapp using Reactjs as frontend & rails api backend? 使用curl访问实现设计的Rails应用程序的后端API - Using curl to access backend API of Rails app that implements devise Fetch api 无法按预期使用 rails 作为后端进行反应 - Fetch api does not work as expected from react using rails as a backend 使用Angular应用程序消耗Rails后端时,如何正确允许CORS? - How do I correctly allow CORS when using an Angular app to consume a Rails backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM