无法从 React Single Page 应用程序进行 api 调用 [英] Unable to make api calls from React Single Page app

查看:21
本文介绍了无法从 React Single Page 应用程序进行 api 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 react-slingshot 样板,并且我的组件(正在进行 api 调用)安装在根组件上.我正在通过导航使用 BrowserRouter.

I am using react-slingshot boilerplate and my component(which is making api call) is mounted on Root Component. I am using BrowserRouter from navigation.

当我从 / 路径拨打电话时,api 工作正常.但是当我从任何其他路线制作时,我收到此错误.

When I am making call from / path , api is working fine. but When i am making from any other route, I am getting this error.

无法加载 https://myapiserver.com/user/:没有访问控制-请求的资源上存在 Allow-Origin' 标头.Origin 'http://localhost:3000' 因此不允许访问.

Failed to load https://myapiserver.com/user/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

我不知道是什么问题.

推荐答案

你的服务器是什么?这是 CORS 问题,因此您可以从 Web 服务器(如 http-server)为所有这些服务.

What's your server? This is a CORS thing, so you can server all this up from a web server, like http-server.

您在这里无事可做.检查 来自 MDN 的此文档.

You have nothing to do here.Check this doc from MDN.

您需要在 Web 服务器的响应中添加标头.例如,请参见以下内容:

You need to add header to your web server's response. For example, see the following:

 <add name="Access-Control-Allow-Origin" value="*" />  
 <add name="Access-Control-Allow-Headers" value="Content-Type" />  
 <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />

根据您的语法更改上述内容.

Change the above according to your syntax.

这篇关于无法从 React Single Page 应用程序进行 api 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆