我正在try 通过我的React应用程序中的Axios进行API调用.然而,我在我的浏览器上看到了这个CORS问题.我想知道我是否可以从客户端解决这个问题,因为我在内部没有访问API的权限.附件是我的密码.

const response = axios({
  method: "post",
  dataType: "jsonp",
  url: "https://awww.api.com",
  data: {
    appToken: "",
    request: {
      applicationName: "ddfdf",
      userName: "jaime@dfd.com",
      password: "dfd",
      seasonIds: [1521ddfdfd5da02],
    },
  },
});

return {
  type: SHARE_REVIEW,
  payload: "response",
};

附件是我的网页.配置.js

module.exports = {
  entry: ["./src/index.js"],
  output: {
    path: __dirname,
    publicPath: "/",
    filename: "bundle.js",
  },
  module: {
    loaders: [
      {
        exclude: /node_modules/,
        loader: "babel",
        query: {
          presets: ["react", "es2015", "stage-1"],
        },
      },
      { test: /\.json$/, loader: "json-loader" },
    ],
  },
  resolve: {
    extensions: ["", ".js", ".jsx"],
  },
  devServer: {
    historyApiFallback: true,
    contentBase: "./",
  },
  node: {
    dns: "mock",
    net: "mock",
  },
};

推荐答案

理想的方法是向服务器添加CORS支持.

您也可以try 使用单独的jsonp模块.据我所知,axios不支持jsonp.因此,我不确定您使用的方法是否符合有效的jsonp请求.

对于CORS问题,还有另一种黑客手段.您必须使用nginx服务器作为服务器和客户端的代理来部署代码.

考虑这个例子.

你的服务器是my-server.com,客户端是my-client.com

// nginx.conf

upstream server {
    server my-server.com;
}

upstream client {
    server my-client.com;
}

server {
    listen 80;

    server_name my-website.com;
    access_log /path/to/access/log/access.log;
    error_log /path/to/error/log/error.log;

    location / {
        proxy_pass http://client;
    }

    location ~ /server/(?<section>.*) {
        rewrite ^/server/(.*)$ /$1 break;
        proxy_pass http://server;
    }
}

此处my-website.com将是可访问代码的网站的最终名称(代理网站的名称).

  • 所有API调用都从my-server.com/<API-path>更改为my-website.com/server/<API-path>

如果你不熟悉nginx,我建议你仔细判断一下documentation.

简要解释上述配置中发生的情况:

  • upstream定义了请求将被重定向到的实际服务器
  • server块用于定义nginx服务器的实际行为.
  • 如果有多个服务器块,server_name用于标识将用于处理当前请求的块.
  • error_logaccess_log指令用于定义日志(log)文件的位置(用于调试)
  • location个块定义了不同类型请求的处理:

Note: 100 here is being used to distinguish the client side requests from the server side requests. Since the domain is the same there is no other way of distinguishing requests. Keep in mind there is no such convention that compels you to add 100 in all such use cases. It can be changed to any other string eg. 102, 103, etc.

尽管这种技术应该可以做到这一点,但我强烈建议您向服务器添加CORS支持,因为这是处理此类情况的理想方式.

如果你想在开发时避免所有这些,你可以使用this个chrome扩展.它应该允许您在开发过程中执行跨域请求.

Reactjs相关问答推荐

为什么这个'Suspense'子组件在挂起promise解决后会丢失它的状态,而不是呈现?<>

在下一个js中使用ESTAccountWithEmailAndPassword函数时循环

从Microsoft Excel粘贴复制的单元格时,将Excel单元格格式(粗体、下划线、斜体)带入Reaction

面对动画警告:未指定`useNativeDriver`.这是必需的选项,并且必须在REACT本机中显式设置为`true`或`False`

不同步渲染

ctx.strokeStyle 在 canvas html 5 中不起作用

状态改变不会触发重新渲染

我如何使用 React toastify (Promise) toast 和邮箱 js

我无法通过 useContext 显示值

我发送的 prop 的值不会改变.如果成功登录,导航栏中的 prop 必须为 true.我从后端得到 200 ok

React-router动态路径

React + Redux:数据未正确传递给具有动态路由的组件

ReactJs 行和列

Cypress ,重试不再附加到 DOM 的元素

react-markdown 不渲染 Markdown

仅在重新渲染后设置状态

gtag:为什么谷歌分析即使没有被授权也会收集数据

由于另一个子组件,如何在react 中渲染另一个子组件

可以'读取未定义的属性(读取'路径')

错误(未捕获的类型错误):无法读取未定义的属性(读取参数)