gpt4 book ai didi

node.js - 参数错误,node.js 中的 options.body?

转载 作者:IT老高 更新时间:2023-10-28 23:03:51 25 4
gpt4 key购买 nike

我不知道是什么问题,请帮助解决这个问题。当我发布数据时,终端上会显示以下错误。附上代码也是。

Error: Argument error, options.body.
at Request.init (/usr/lib/nodejs/request/index.js:351:13)
at new Request (/usr/lib/nodejs/request/index.js:124:8)
at Object.request (/usr/lib/nodejs/request/index.js:1279:11)
at Request._callback (/var/www/html/nodeproject/helloworld/controllers/login.js:68:11)
at Request.self.callback (/usr/lib/nodejs/request/index.js:148:22)
at Request.EventEmitter.emit (events.js:98:17)
at Request.<anonymous> (/usr/lib/nodejs/request/index.js:896:14)
at Request.EventEmitter.emit (events.js:117:20)
at IncomingMessage.<anonymous> (/usr/lib/nodejs/request/index.js:847:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)

下面是我的代码,这里使用 json 发布数据。

module.exports.controller = function(BASE) {
var token_array = new Array();

BASE.APP.get('/login', function(req, res)
{
res.render('pages/login');
//res.sendFile(__dirname + '/login.html');

});



BASE.APP.post("/login", BASE.urlEncodedParser, function(req, response)
{

var devicetoken = req.usersession.devicetoken;
var body = req.body;
req.usersession.username = body.username;

console.log(req.body);
var userData={
"deviceToken" : devicetoken,
"password" : body.pwd,
"username" : body.username
};
var digest = BASE.utils.hmac("sha1", "A12AA418-1F28-4464-8B67-29CBD02BC45C-F048B14F-F3E3-4F97-A522-F2275A364A0E", JSON.stringify(userData));
var postData = {
"deviceToken" : devicetoken,
"password" : body.pwd,
"username" : body.username,
"digest" : digest
};

var sPostData = BASE.utils.base64encode(BASE.utils.base64encode(JSON.stringify(postData)));
BASE.request({
url: "http://example.com/authenticate/",
method: "POST",
headers: {
"content-type": "application/json",
},
body: sPostData
},
function(err,result,body){
//console.log(JSON.stringify(postData));

var body = JSON.parse(body);
req.usersession.token = body.token;

/*********************************has login permission***************************************/
var loginData={


"permission" : '08008749-F3A5-480B-A2B2-C21CEFED70F4',
"token" : req.usersession.token

};



var logindigest = BASE.utils.hmac("sha1", "A12AA418-1F28-4464-8B67-29CBD02BC45C-F048B14F-F3E3-4F97-A522-F2275A364A0E", JSON.stringify(loginData));
var loginpostData = {

"permission" : '08008749-F3A5-480B-A2B2-C21CEFED70F4',
"token" : req.usersession.token,
"digest" : logindigest

};

BASE.request({
url : "http://example.com/hasPermission/",
method :"POST",
headers : {

"content-type": "application/json",
},
body :loginpostData
},
function (err,result1,body) {

console.log(body);

});

//response.redirect(301, '/contacts');
});

});

}

我正在使用另一个文件的相同类型代码,它不应该给出任何错误,但是这个文件告诉我

最佳答案

尝试在您的请求选项中添加 json: true

例如:

BASE.request({
url : "http://example.com/hasPermission/",
method :"POST",
headers : {
"content-type": "application/json",
},
body: loginpostData,
json: true
},

关于node.js - 参数错误,node.js 中的 options.body?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35598753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com