使用POST HTTP请求中的回调函数后保存JSON对象

时间:2018-11-25 15:30:42

标签: node.js http callback request

我希望能够将“ answer”用作任何其他函数中的变量。我该怎么办?

代码:

var answer = function name("happy", function(body){console.log(body) });

1 个答案:

答案 0 :(得分:0)

很难理解您想要达到的目标,但是如果我猜对了,您的状态很好。我认为您已经可以做到,只是您没有在代码中。这就是你想要的吗?

function getJSON(input, callback) {
    var all = {'documents': [...]};
    request({
        headers: {'Host': 'westus.api.cognitive.microsoft.com'},
        uri: 'westus.api.cognitive.microsoft.com/text/analytics/v2.0/…',
        json: true, body: all, method: 'POST'
        },
        function (error, response, body) {
            if (error) {callback(error || {statusCode: response.statusCode});} else callback(body);
        });
}

function myOtherFunction(bodyOrError){
    console.log(bodyOrError);
};

getJSON('testInput', myOtherFunction);

还有一件事:您以非常不可读的方式发布了代码,请编辑您的初始问题,然后将所有代码发布到那里。