使用node.js中的请求模块无法执行POST请求 [英] POST Requests not working using request module in node.js

查看:43
本文介绍了使用node.js中的请求模块无法执行POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据发送到在tomcat服务器上运行的java后端.这是我到目前为止已经尝试过的方法.我已经安装了request module.get方法可以正常工作.

I want to send data to the java back end which is run on tomcat server.this is what i have tried so far.i have already installed request module.get method is working properly.

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);
    });

});

推荐答案

您必须使用JSON.stringify以这种格式发送数据.在那之前写console.log(error).并检查您得到的错误是什么.

You have to use JSON.stringify to send data like in this format. before that write console.log(error). and check what's the error you are getting.

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);
        }
    });

这篇关于使用node.js中的请求模块无法执行POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆