【问题标题】:Sending data from Node.js back to client?将数据从 Node.js 发送回客户端?
【发布时间】:2015-11-01 13:20:06
【问题描述】:

我正在从客户端向 Node.js 发送 POST 请求。在请求的处理程序中,我正在使用数据发出 HTTP POST 请求(在 Node.js 中),这给了我一个 JSON 数据作为响应,然后我又用该数据发出另一个 HTTP POST 请求(在 Node.js 中)这给了我一组数据作为回应。现在我想将此响应数据返回给处理函数,以便接收到的数据集作为响应发送回客户端。我怎样才能做到这一点?

server.route({
path:"/test",
method:"POST",
handler:function(request,reply){
    var load=request.payload;
    UserAuth(load);
    return reply({status:"Ok"});
    }

 });
function UserAuth(newdata){

request({   
    har:{
    url:"URL",
    method:"POST",
    headers:[
        {
        name:'content-Type',
        value:"application/x-www-form-urlencoded;charset=utf-8"
    }
    ],
    postData:{
        mimeType: 'application/x-www-form-urlencoded',
        params:[
            {
                name:"username",
                value:UserDetails["username"]
            },
            {
                name:"password",
                value:UserDetails["password"]
            
            },
            {
                name:"output_mode",
                value:UserDetails["output_mode"]
            
            }
        ]
    }   
}
},function(error,response,body){
    
    var obj = JSON.parse(body);
    if(obj.sessionKey != null || obj.sessionKey!=""){
         PostDataToS(newdata,obj.sessionKey);
    
    }else{
    
        console.log(error);
    }
    
});

}
 function PostDataToS(data,sessionkey){

request({   
    har:{
    url:SEARCH_URL,
    method:"POST",
    headers:[
        {
        name:'content-Type',
        value:"application/x-www-form-urlencoded;charset=utf-8"
    },
        {
            name:"Authorization",
            value:sessionkey
    }
    ],
    postData:{
        mimeType: 'application/x-www-form-urlencoded',
        params:[
            {
                name:"search",
                value:data["search"]
            },
            {
                name:"preview",
                value:"false"
            
            },
            {
                name:"output_mode",
                value:"json"
            
            },
            {
                name:"exec_mode",
                value:"oneshot"
            
            }
        
        ]
    }   
}
},function(error,response,body){
    
    obj2 = JSON.parse(body);
    var secondLayer=obj2.results;
    returnfunc(secondLayer);
    
 });

}

function returnfunc(data){
console.log("inside return func");
console.log(data)

}

我必须将在returnfunc() 中收到的数据发送回/test 的请求处理程序中的客户端。

【问题讨论】:

  • 我在 node.js 之上使用 hapi.js 框架

标签: javascript node.js post client


【解决方案1】:

只需将回复函数传递给您的回调函数

server.route({
    path:"/test",
    method:"POST",
    handler:function(request,reply){
        var load=request.payload;
        UserAuth(load);
    }
});
function UserAuth(newdata, reply){
    request({...},function(error,response,body){
        var obj = JSON.parse(body);
        if(obj.sessionKey != null || obj.sessionKey!=""){
            PostDataToS(newdata,obj.sessionKey, reply);
        } else {
            console.log(error);
        }
    });
}
function PostDataToS(data, sessionkey, reply){
    request({...},function(error,response,body){
        obj2 = JSON.parse(body);
        var secondLayer=obj2.results;
        reply.continue(obj2.results);
    });
}

【讨论】:

  • 但数据仍然没有返回到客户端。请求成功,是的,我应用了您的方法传递回调函数。我能够在服务器级别看到数据。但是客户端级别的 ajax 请求处理程序处的数据给我空白。
  • 我的错,对不起,我的错。有效 。太感谢了 。我很感激。
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2013-02-01
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多