Javascript 将request.post中的body(html)响应保存/使用到nodejs中的全局变量中
我想将body的内容保存到我的全局变量Javascript 将request.post中的body(html)响应保存/使用到nodejs中的全局变量中,javascript,node.js,scope,request,http-post,Javascript,Node.js,Scope,Request,Http Post,我想将body的内容保存到我的全局变量returnData中,以便将其返回到外部函数。但是,它只有在回调内部使用Iconsole.log时才起作用。数据不会保存到全局变量中 var returnData; // This is where I want to store the output of request.post function getDataFromPostRequest() { request.post( { u
returnData
中,以便将其返回到外部函数。但是,它只有在回调内部使用Iconsole.log
时才起作用。数据不会保存到全局变量中
var returnData; // This is where I want to store the output of request.post
function getDataFromPostRequest() {
request.post(
{
url: "http://example.com",
formData: {x: "y"},
},
function optionalCallback(err, httpResponse, body) {
console.log(body) // this is working
returnData = body;
}
);
}
console.log("returnData ", returnData); // This is undefined
也许你能做到:
var returnData; // This is where I want to store the output of request.post
function getDataFromPostRequest() {
request.post(
{
url: "http://example.com",
formData: {x: "y"},
},
function optionalCallback(err, httpResponse, body) {
console.log(body) // this is working
returnData = body;
console.log("returnData ", returnData);
}
)
}
也许你能做到:
var returnData; // This is where I want to store the output of request.post
function getDataFromPostRequest() {
request.post(
{
url: "http://example.com",
formData: {x: "y"},
},
function optionalCallback(err, httpResponse, body) {
console.log(body) // this is working
returnData = body;
console.log("returnData ", returnData);
}
)
}
感谢您的回答,但问题是为什么外部范围中的
未定义
,以及我们如何在请求之外使用主体
。post
感谢您的回答,但问题是为什么外部范围中的未定义
,以及我们如何在外部范围之外使用主体
request.post