Axios出现CORS问题 [英] Axios having CORS issue

查看:3739
本文介绍了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

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天全站免登陆