NodeJS http发布数据为空

时间:2017-01-08 03:07:53

标签: node.js angular http-post

我无法在express nodejs的http post方法中获取数据。我正在从angular2发布数据。当我在chrome的网络部分进行检查时,有效负载是可见的,但app.post方法会收到相同的数据空白。请帮助我。

angular2代码

#import Points from CSV

import rhinoscriptsyntax as rs
import sys
import datetime

input_file = 'C:\Users\kenma\Dropbox (Personal)\Solo Work\Projects\Sweet Crude\Work\data\prepared_uic_data.csv'

#Init Lists
a = []
apis = []           #0
operators = []      #1
operatorNums = []   #2
wellTypes = []      #3
dates = []          #4
lats= []            #5
longs = []          #6
zoneAreas = []      #7
dateFrag = []
dateM = []          #8
dateD = []          #9
dateY = []          #10

file = open(input_file, 'r')    #open file for reading
lines = file.readlines()        #read lines into variable
file.close()                    #close the file
del lines[0]                    #delete first header line

for line in lines:
    #remove the /n
    line = line.strip()

    # split line by the column
    ptInfo = line.split(',')
    a = ptInfo

    # split line data into individual arrays
    apis.append(ptInfo[0])
    operators.append(ptInfo[1])
    operatorNums.append(ptInfo[2])
    wellTypes.append(ptInfo[3])
    dates.append(ptInfo[4])
    lats.append(ptInfo[5])
    longs.append(ptInfo[6])
    zoneAreas.append(ptInfo[7])

    dateFrag = ptInfo[4].split("/")
    print(dateFrag[0])
    print(dateFrag[1])
    print(dateFrag[2])

nodejs代码

  this.headers = new Headers();
  this.headers.append('Content-Type', 'x-www-form-urlencoded');

  let body = JSON.stringify({name:"Lionel Messi"});



 return this.http
.post('http://localhost:8081/save',body
     ,this.headers);   

} 

Network Section in Chrome....payload is proper

2 个答案:

答案 0 :(得分:0)

node.js中的

alert方法不起作用。您需要使用console.log("Hello");

第二件事是获取身体数据,使用req.body.name

我编写代码的方式如下所示

$http({
    method: 'POST',
    url: 'http://localhost:8081/save',
    data: {name:"Lionel Messi"}
})
.success(function(data) {
     return data
  })
.error(function(error) {
     // handle error
});

您可以尝试的其他方式是:

$http.post('http://localhost:8081/save', {name:"Lionel Messi"})
.then(function(data) {return data})   
.catch(function() {console.log("Error Occured");});

答案 1 :(得分:0)

你可以这样做 - 假设您已通过邮寄方式从浏览器发送用户名和密码。

app.post("/ url,function(request,response)
{ var username=request.body.username;
var password=request.body.password;})