简体   繁体   English

从node.js发送响应到Jquery

[英]send response from node.js to Jquery

Iam new to node.js I want to get data fetch from db and send that returned value to Jquery. 我是node.js的新手,我想从db获取数据并将该返回值发送到Jquery。 in the following code 在以下代码中

exports.signin = function (signupCollection, context) {
var record = utils.httpBody(context.req);
signupCollection.find({ },{email: record.email,password: record.password},function(err, result) {
      console.log("result-------"+result) // prints the data correctly
      if(!err) return result;
    });    
}

result prints the correct data i want...it is getting properly result打印出我想要的正确数据...它正在正确

In jquery 在jQuery中

var data = {   
    "email": $('#email').val(),
    "password": $('#password').val()   
   };
        $.ajax({
            type: "post",
            url: "/signIn",
            dataType: "json",
            data: data,
            success: function (data) {
                console.log("sign in succesfully!!"); // here i want to  getthe result data
                console.log("SIGN IN RESPOSEE----"+data);             

            }
        });

inside this i couldn't get the resultant data returned from node,js 在这个里面我无法得到从node,js返回的结果数据

success: function (data) {
alert("data-------------"+data);
}

You should have written the result to http.ServerResponse ( response object) and call response.end() . 您应该已经将结果写入http.ServerResponseresponse对象)并调用response.end()

Are you using any web framework? 您正在使用任何Web框架吗? My guess is context.res would be the response object. 我的猜测是context.res将是response对象。

Use: 采用:

return context.res.end(JSON.stringify(result));

instead of 代替

return result;

Reference: 参考:

http://nodejs.org/api/http.html#http_class_http_serverresponse http://nodejs.org/api/http.html#http_class_http_serverresponse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM