我如何在node.js中获取请求主体?

时间:2016-12-12 09:03:26

标签: javascript node.js xml http

客户端如何向服务器发送数据。运行时我能检查bcoz这是生产服务器代码

var request = new XMLHttpRequest();
request.open('POST', 'http://localhost:3000/userdata', true);
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(gRequestString);

gRequestString := '{"CustomerData":{';
gRequestString := gRequestString + '"CustomerChannel":"Test",';
gRequestString := gRequestString + '"CustomerCredential":"Testing",';
gRequestString := gRequestString + '"CustomerId":"Testing",';
gRequestString := gRequestString + '"DeviceId":"' + pTransHeader."Test Terminal No." + '",';
gRequestString := gRequestString + '"CustomerStoreId":"' + pTransHeader."Store No." + '",';
gRequestString := gRequestString + '"CustomerUserId":"' + pTransHeader."Staff ID" + '"},';

gRequestString := gRequestString + '"CustomerData":{"CustomerId":"' + pTransHeader."Cust. Phone No." + '"},';

gRequestString := gRequestString + '"ErnD":{"UID":"' + FORMAT(pTransHeader."Transaction No.") + '",';
gRequestString := gRequestString + '"TxnDt":"' + FORMAT(pTransHeader.Date, 0, '<Year4>-<Month,2>-<Day,2>');
gRequestString := gRequestString + '",';
gRequestString := gRequestString + '"TxnTm":"' +
                                   FORMAT(pTransHeader.Time, 0, '<Hours24>:<Minutes,2>:<Seconds,2>') + '",';
gRequestString := gRequestString + '"TxnVal":"' + FORMAT(pTransHeader."Gross Amount") + '",';
gRequestString := gRequestString + '"TxnInv":"' + pTransHeader."Receipt No." + '"},';
gRequestString := gRequestString + '"PurD":[';

gTransSalesEntry.SETCURRENTKEY("Receipt No.");
gTransSalesEntry.SETRANGE("Receipt No.", pTransHeader."Receipt No.");
IF gTransSalesEntry.FINDSET THEN BEGIN
  CLEAR(gTempStr);
  i := 1;
  REPEAT
    IF MAXSTRLEN(gTempStr[i]) - STRLEN(gTempStr[i]) <= 23 THEN
      i += 1;
    gTempStr[i] := gTempStr[i] + '"' + gTransSalesEntry."Item No." + '|';
    gTempStr[i] := gTempStr[i] + FORMAT(-1 * gTransSalesEntry.Quantity) + '|';

    lNetAmount := FORMAT(-1 * gTransSalesEntry."Net Amount");
    lNetAmount2 := '';
    FOR j := 1 TO STRLEN(lNetAmount) DO BEGIN
      lChar := COPYSTR(lNetAmount, j, 1);
      IF lChar <> ',' THEN
        lNetAmount2 := lNetAmount2 + lChar;
    END;
    gTempStr[i] := gTempStr[i] + lNetAmount2 + '",';
  UNTIL gTransSalesEntry.NEXT = 0;
  gTempStr[i] := COPYSTR(gTempStr[i], 1, STRLEN(gTempStr[i]) - 1);
  gTempStr[i] := gTempStr[i] + ']}';
END;

lTotalLen := STRLEN(gRequestString) + STRLEN(gTempStr[1]) + STRLEN(gTempStr[2]);

IF lTotalLen < 2044 THEN BEGIN
XmlHttp.send(gRequestString + gTempStr[1] + gTempStr[2]);

这是我在控制器中写的方式我想知道如何在控制台日志中获取json任何人帮助我

exports.create = function(req, res) {
console.log(req.body);
  Userdata.create(req.body, function(err, Userdata) {
    if(err) { return handleError(res, err); }
    return res.status(201).json(Userdata);
  });
};

0 个答案:

没有答案