2017-07-05 155 views
-1

我想发送数据到在tomcat server.this上运行的java后端,这是我迄今试过的。我已经安装了请求module.get方法是好好工作。POST请求无法使用node.js中的请求模块工作

Router.post('/', function(req, res) { 

    request({ 
     uri: "http://localhost:8080/HIS_API/rest/UserService/registerUser", 
     method: "POST", 
     form: { 
      roleId:2, 
      employeeId:26, 
      userName:"testing", 
      password:"123" 
     } 
    }, function(error, response, body) { 
     console.log(body); 
    }); 

}); 
+0

那到底究竟是不是工作?你有错误吗?也许服务器期望除了'application/x-www-form-urlencoded'之外的东西? – robertklep

回答

0

你必须使用JSON.stringify来发送像这种格式的数据。在此之前编写console.log(错误)。并检查你得到的错误是什么。

request({ 
     url: url, //URL to hit 
     method: 'post', 
     headers: { "Authorization": req.headers.authorization},//if required 
     timeout: 60 * 1000, 
     body: JSON.stringify(body) 
    }, function (error, result, body) { 
     if (error) { 
      console.log(error); 
     } else if (result.statusCode === 500) { 
      console.log('error'); 
     } else { 
      console.log(body); 
     } 
    });