简体   繁体   English

跨源请求被阻止:同源策略不允许读取远程资源 - react js

[英]Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource - react js

I run my project on my Mac OS device and I want to access from another laptop.我在我的 Mac OS 设备上运行我的项目,我想从另一台笔记本电脑访问。

the first device gets all responses from the server as well:第一个设备也从服务器获得所有响应:

http://192.168.1.101:3000/ http://192.168.1.101:3000/

but another laptop I got this error message:但另一台笔记本电脑我收到此错误消息:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.shadyab.com/api/Api/coupons. (Reason: missing token 'access-control-allow-origin' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel).

const requestOptions = { method: 'POST', headers: { 'Content-Type': 'multipart/form-data', 'Access-Control-Allow-Origin': '*'}, body: JSON.stringify(formData) };

Add

headers: {'Access-Control-Allow-Origin': '*'}

to your server where the API is fetching from 到您要从中获取API的服务器

I think this is something related to your backend sometimes backend only allows some origins and your new front-end domain must be added to Access-Control-Allow-Origin我认为这与您的后端有关,有时后端只允许某些来源,您的新前端域必须添加到 Access-Control-Allow-Origin

but sometimes that could be related to the webserver and its configuration needs to be change, for example if you are using Apache .htaccess file must be changed但有时这可能与网络服务器有关,并且需要更改其配置,例如,如果您使用的是 Apache .htaccess,则必须更改文件

Assuming you are using cors() in the backend (like in a node server).假设您在后端使用 cors() (如在节点服务器中)。 Then in your react app, what you can do is setup proxy for the api endpoints.然后在您的 React 应用程序中,您可以为 api 端点设置代理。

in the src directory create a file named setupProxy.js .src目录中创建一个名为setupProxy.js的文件。 What it does is, create proxies for your api endpoints.它的作用是为您的 api 端点创建代理。 What you can do is something like below你可以做的是像下面这样的事情

setupProxy.js setupProxy.js

const { createProxyMiddleware } = require('http-proxy-middleware');

const BACKEND_HOST = process.env.REACT_APP_BACKEND_HOST || 'localhost';
const BACKEND_PORT = process.env.BACKEND_PORT || 8000;

module.exports = function(app) {

  app.use(
    '/',
    createProxyMiddleware({
      target: target,
      changeOrigin: true,
      logLevel: 'debug'
    })
  );

  /**
  *   You can create other proxies using app.use() method.
  */
};

Note: You do not need to import this file anywhere.注意:您不需要在任何地方导入此文件。 It is automatically registered when you start the development server.当您启动开发服务器时,它会自动注册。

And after creating proxies, you should send request to your backend server only specifying the endpoints.创建代理后,您应该仅指定端点向后端服务器发送请求。 Like if you want to send request you should use / instead of http://localhost:8000 .如果你想发送请求,你应该使用/而不是http://localhost:8000

Let me if it works.让我如果它有效。 Thanks谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 跨域请求被阻止:“相同来源策略”不允许读取远程资源 - Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource a 为什么我收到跨域请求被阻止的原因:同源策略禁止读取远程资源 - Why do I get a Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource Cross-Origin Request Blocked:同源策略不允许读取远程资源。 CORS 预检响应未成功 - Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource. CORS preflight response did not succeed Cross-Origin Request Blocked:同源策略不允许读取远程资源。 (原因:CORS 预检响应没有成功) - Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource. (Reason: CORS preflight response did not succeed) 跨域请求被阻止:同源策略不允许读取位于 https://localhost:8000/users/login 的远程资源 - Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:8000/users/login 同一起源策略不允许读取远程资源 - The Same Origin Policy disallows reading the remote resource 浏览器中的“跨源请求被阻止:同源策略”错误 - “Cross-Origin Request Blocked: The Same Origin Policy” Error in browser 跨域请求被阻止 Django 和反应 - Cross-Origin Request Blocked Django and React Angular JS的跨域请求被阻止 - Cross-Origin Request Blocked for Angular JS 使用 React 和 Express 阻止跨源请求 - Cross-Origin Request Blocked with React and Express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM