Axios 有 CORS 问题 [英] Axios having CORS issue

查看:71
本文介绍了Axios 有 CORS 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 package.json 中添加了代理并且效果很好,但是在 npm run build 之后 CORS 问题再次浮出水面,有没有人知道在 React 中 npm run build 之后如何处理 CORS 问题.

I added proxy in package.json and it worked great, but after npm run build the CORS issue has resurfaced again, does anyone know how to deal with CORS issue after npm run build in React.

我尝试使用各种方法在 axios 请求中添加标头.但是,我未能在 axios 请求中添加 'Access-Control-Allow-Origin':'*' .我的代码如下:

I have tried to add headers in axios request using various methods. However, I failed to add 'Access-Control-Allow-Origin':'*' in axios request. My code is as follwing:

package.json

package.json

  "proxy": {
      "*":{ "target" : "http://myurl"}
   } 

GetData.js

  axios.defaults.baseURL = 'http://myurl';
  axios.defaults.headers.post['Content-Type'] ='application/json;charset=utf-8';
  axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
  axios.get(serviceUrl, onSuccess, onFailure)
  .then(resp => { 
        let result = resp.data;
        onSuccess(result);
  })
  .catch(error => {
        if(onFailure) {
            return onFailure(error);
        }
  })
 }

注意:它已经从服务器端启用,它仍然无法工作.目前,我无法从服务器端更改代码,我的工作仅限于客户端.

Note: It has enabled from server side, it is still not working.Currently, I can't change code from server side, My work is limited to client side only.

推荐答案

您的服务器应该启用跨源请求,而不是客户端.为此,您可以查看这个不错的页面,其中包含多个平台的实现和配置

your server should enable the cross origin requests, not the client. To do this, you can check this nice page with implementations and configurations for multiple platforms

这篇关于Axios 有 CORS 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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