Node.js HTTP post请求,回调数据老是报错Can't set headers after they are sent
发布于 8 年前 作者 Wizzercn 4806 次浏览 来自 问答

Error: Can’t set headers after they are sent.

var httpreq = require('request');
....
..
kuaidi: function (req, res) {
    var ShipperCode = 'SF';
    var LogisticCode = '033813206167';
    if (sails.config.system.ShopConfig.logistics_open && sails.config.system.ShopConfig.logistics_info) {
      var logistics_info = sails.config.system.ShopConfig.logistics_info;
      if ('kdn' == logistics_info.lg_appname) {
        var obj = {
          OrderCode: '',
          ShipperCode: ShipperCode,
          LogisticCode: LogisticCode
        };
        var RequestData = JSON.stringify(obj);
        var DataSign = StringUtil.base64Encode(md5(RequestData + logistics_info.lg_appkey));
        httpreq.post('http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx',{form:{
          'EBusinessID': logistics_info.lg_appid,
          'RequestType': '1002',
          'RequestData': encodeURI(RequestData),
          'DataSign': DataSign,
          'DataType': '2'
        }}, function (e, r, body) {
          if (!e && r.statusCode == 200) {
            return res.json({code: 0, msg: '', data: JSON.parse(body)});//这里报错啊。。。
          }else{
            return res.json({code:1,msg:'err'});
          }
        });
      } return res.json({code:1,msg:'err'});
    } else
      return res.json({code:1,msg:'err'});
  }
2 回复

啊 啊 能不能删,,发现问题了,,少打个else

你这代码还有待格式化。 报错的原因是 因为你倒数第二个res.json之后,异步的httpreq.post执行完了之后 在res.json就报错了。

回到顶部