2013-09-24 49 views
0

Iam new to node.js 我想從數據獲取數據庫並將返回的值發送到Jquery。 下面的代碼發送從node.js到Jquery的響應

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打印正確的數據,我想......它是正確

讓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);    

      } 
     }); 

這裏面我不能得到結果數據返回來自節點,js

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

回答

1

你應該寫結果爲http.ServerResponseresponse對象),並致電response.end()

您是否使用任何Web框架?我的猜測是context.res將是response對象。

用途:

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

,而不是

return result; 

參考:

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

+0

yessss ... context.res是object.how我可以修改代碼的響應像你說的? – Psl

+0

正在獲取http.js:593 throw new TypeError('第一個參數必須是字符串或緩衝區'); – Psl

+0

'result'似乎是一個'Object',使用'JSON.stringify()'。 – leesei