Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 请求。发布如何获取尸体返回?_Javascript_Node.js_Request - Fatal编程技术网

Javascript 请求。发布如何获取尸体返回?

Javascript 请求。发布如何获取尸体返回?,javascript,node.js,request,Javascript,Node.js,Request,我做了一个关于外部post请求的测试。因此,我以这种方式启动我的应用程序: var http = require('http'); var extreq = require('./app/external-request'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); var aaa = extreq.

我做了一个关于外部post请求的测试。因此,我以这种方式启动我的应用程序:

var http = require('http');
var extreq = require('./app/external-request');

http.createServer(function (request, response) {

     response.writeHead(200, {'Content-Type': 'text/plain'});

     var aaa = extreq.login();

     response.end(JSON.stringify(aaa));

}).listen(1337, "127.0.0.1");
在“external request.js”中,我有:


我的问题是,如何在这里获得“var aaa=extreq.login();”的“return json”?

这里的问题是
request.post()
。这是一种异步方法,因此登录函数在
请求.post()调用完成之前完成并返回。下面将显示实际情况:

function login() {
    // obviously, you'll need to make sure you include whatever resources
    // you need from 'external-request.js' before this
    request.post(url, { form: { userid: 'myuserid', password: '*****' } },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var json = JSON.parse(body);
                // this will log *after* the note in your main response body
                console.log('this is the second thing your code will log to the console');
                console.log(json); // Work
                // this return doesn't actually go anywhere, it's just lost
                return json; // Not work
            }
    });
    return { whoops: 'this is what we really return' };
}

var http = require('http');

http.createServer(function (request, response) {

    response.writeHead(200, {'Content-Type': 'text/plain'});

    var aaa = extreq.login();

    console.log('this is the first thing your code will log to the console');

    response.end(JSON.stringify(aaa));

}).listen(1337, "127.0.0.1");
。。。运行它,您将看到您的代码并不像您期望的那样执行

当人们提到使用回调时,他们的意思是,当
request
在回调函数中发布完外部请求后,您需要打包您希望实现的功能,然后它可以在实际准备就绪时执行其工作。如下所示:

var http = require('http');
var extreq = require('./app/external-request.js');

http.createServer(function (request, response) {

    response.writeHead(200, {'Content-Type': 'text/plain'});

    extreq.login(function(aaa) {
        response.end(JSON.stringify(aaa));
    });
    console.log('this is the first thing your code will log to the console');
}).listen(1337, "127.0.0.1");
。。。然后,您的登录功能:

function login(callback) {
    request.post(url, { form: { userid: 'myuserid', password: '*****' } },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var json = JSON.parse(body);
                // this will log *after* the note in your main response body
                console.log('this is the second thing your code will log to the console');
                console.log(json); // Work
                // bonus points: pass in body directly, and skip stringify
                callback(json);
            }
    });
    return { whoops: 'this is what we really return' };
}

你不能。您必须设计API,以便传入回调函数。然后,您的“login”函数可以将对象传递到该回调中。我尝试了如下操作:函数回调(error,response,body){if(!error&&response.statusCode==200){var info=JSON.parse(body);console.log(info.stargazers_count+“Stars”);console.log(info.forks_count+“forks”);}request.post(选项、回调);但我也有同样的问题。所以我尝试了:login(res){res.end(JSON.stringify(JSON));}但是没有什么可以做的。您必须更改“login()”函数,以便可以将回调传递给它。请将问题链接为副本。谢谢。还有关于如何显示“response.end(JSON.stringify(aaa));“on body”response.end('my JSON');”?我不太清楚您在这里要求什么。
function login(callback) {
    request.post(url, { form: { userid: 'myuserid', password: '*****' } },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var json = JSON.parse(body);
                // this will log *after* the note in your main response body
                console.log('this is the second thing your code will log to the console');
                console.log(json); // Work
                // bonus points: pass in body directly, and skip stringify
                callback(json);
            }
    });
    return { whoops: 'this is what we really return' };
}