在 create-react-app 托管服务器上使用代理的 Axios 帖子不起作用 [英] Axios post with proxy on create-react-app hosting server doesn't work

查看:22
本文介绍了在 create-react-app 托管服务器上使用代理的 Axios 帖子不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 package.json (react app) 中有以下行

I have following line in my package.json (react app)

"proxy": "http://www.demoapp.com"

我正在使用 REST-API 调用请求数据

I'm requesting data using REST-API call

axios.post('/user_data', { // http://www.demoapp.com/user_data
        id:id
    })
    .then((response) => { 
        console.log(response);
    })
    .catch((error) => {

    });

如果我在本地系统中使用上面的代码可以正常工作,但是如果我构建一个项目并上传到托管服务器代理则不起作用.

Above code works fine if I use it in my local system, But if I build a project and upload into hosting server proxy doesn't work.

有人知道解决办法吗?我也检查了 github 但没有找到合适的解决方案.

Anyone know the solution? I have checked in github also but not found proper solution.

推荐答案

我认为你应该在 axios 配置中定义你的代理,如果它应该全局应用于每个请求,你可以使用此代码定义全局 axios 默认https://github.com/axios/axios#config-defaults

I think you should define your proxy in the axios config, if it should be applied globally for every requests, you can use this code to define the Global axios default https://github.com/axios/axios#config-defaults

import axios from 'axios';

axios.defaults.proxy.host = "http://www.demoapp.com"
axios.defaults.proxy.port = ...
axios.defaults.proxy.auth.username = ...
axios.defaults.proxy.auth.password = ...

这篇关于在 create-react-app 托管服务器上使用代理的 Axios 帖子不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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