vue项目中的vue.config.js配置文件中的proxy代理post请求时报ECONNRESET错误

1、问题

错误如下所示:
在这里插入图片描述
在这里插入图片描述
vue.config.js中的代理配置如下:

devServer: {
	proxy: {
      // change xxx-api/login => mock/login
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      '/ecustom': {
        target: 'http://xxx.xx.x.xxx:xx', //API服务器的地址
        // ws: true, //代理websockets
        changeOrigin: true, // 是否跨域,虚拟的站点需要更管origin
        pathRewrite: {
          // '^/api5'是一个正则表达式,表示要匹配请求的url中,全部'http://localhost:8081/api5' 转接为 http://localhost:8081/api/
          '^/ecustom': '',
        },
        logLevel: 'debug' // 打印调试信息
      },
   }
}

2、分析

(1)在postman上测试接口,数据能正常返回,说明接口是没问题。
(2)代理了get请求也能够请求成功,说明代理配置上也是没问题的。
(3)猜测可能是代理后post的数据格式上有问题。

3、解决

配置onProxyReq去处理返回的数据。
vue-config.js中的代理配置代码如下:

devServer: {
    proxy: {
      '/ecustom': {
        target: 'http://xxx.xx.x.xxx:xx', //API服务器的地址
        // ws: true, //代理websockets
        changeOrigin: true, // 是否跨域,虚拟的站点需要更管origin
        onProxyReq: function (proxyReq, req, res, options) {
          if (req.body) {
            let bodyData = JSON.stringify(req.body);
            // incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
            proxyReq.setHeader('Content-Type','application/json');
            proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
            // stream the content
            proxyReq.write(bodyData);
          }
        },
        pathRewrite: {
          // '^/api5'是一个正则表达式,表示要匹配请求的url中,全部'http://localhost:8081/api5' 转接为 http://localhost:8081/api/
          '^/ecustom': '',
        },
        logLevel: 'debug' // 打印调试信息
      },
    }
  },

4、结果

在这里插入图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值