2013-05-28 23 views
0

所以我已經找遍了答案,似乎無法找到一個請求體是一個空數組,快遞Node.js的REST

我正在做一個應用程序的NodeJS,在有一點我想以JSON的形式將數據發佈到我的服務器。以下是我在我的「users.js」在服務器上:

/** Updates the array with the new user information **/ 
exports.addUser = function(req, res) { 
var user = req.body; 
var userDB = findByEmail(user.email); 
if(userDB == -1){ 
     //New user - send back user information 
      users.push(user); 
      res.send(JSON.stringify(user)); 

每當我檢查瀏覽器 - 我看到的是,雖然我發送corrent信息,響應只是{}

下面是我對client.js

var user = {email:userEmail,username:userUsername,password:userPassword}; 
request = new XMLHttpRequest(); 

    if (request) 
    { 
     request.open("POST","users"); 
     request.onreadystatechange = function() 
     { 
      if (request.readyState == 4 && 
        request.status == 200){ 
        result = JSON.parse(request.responseText); 

        if(result == 0) 
        { 
         //Result came back as 0 - Old User, wrong password 
        } 
       if(result == 1){ 
          //Result came back as 1 - Correct password, old User 

         } 

         if(result.email == userEmail){ 
          //Result came back as -1 - New User 

          } 
      } 
     } 

     request.send(JSON.stringify(user)); 
    } 

現在這裏的一切,我認爲是正確的 - 因爲我看到HTTP請求出來好。 然而,它出現在請求的'有效載荷'部分 - 這是正常的嗎?

這是我需要在server.js -

var express = require('express'); 
var questions = require('./Quotes'); 
var scoreboard = require('./scoreboard'); 
var users = require('./users'); 
var app = express();//.createServer(); 
app.configure(function() { 
    app.use(express.logger('dev'));  
    app.use(express.bodyParser());//it will parse json request bodies (as well as others), and place the result in req.body: 
}); 

app.post('/users',users.addUser); 

最後這裏是什麼瀏覽器顯示時,我測試了 -

我忘了補充 - 這是要求外觀上的瀏覽器 -

Accept:*/* 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:70 
Content-Type:application/xml 
Host:whosaidit.eu01.aws.af.cm 
Origin:http://whosaidit.eu01.aws.af.cm 
Referer:http://whosaidit.eu01.aws.af.cm/ 
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36 
Request Payloadview source 
{email:[email protected], username:orepor, password:yotamp} 
email: "[email protected]" 
password: "password" 
username: "john123" 

請幫忙!

感謝

回答

0

有幾件事情我注意到:

  • 即使你是你的客戶端數據進行編碼以JSON,你不告訴它的JSON,你是服務器發送給它。在您發送您的請求,將內容類型:
request.setRequestHeader('content-type', 'application/json'); 
    request.send(...); 
  • 如何findByEmail()實施?如果它查詢數據庫,那麼它有可能是異步實現的,這意味着你不能指望它返回有用的返回碼。

  • 這段代碼是混亂:

if (userDB == -1) { 
    users.push(user);    // what is 'users'? what's the use of this code? 
    res.send(JSON.stringify(user)); // you send back an object, but in your 
            // client side code you are expecting a 
            // number: "if (result == 0)" 
    } 
+0

這是因爲我一直在玩了,我會返回一個數字,但現在我只是想看看有什麼「用戶「是我發送的。截至目前,我沒有數據庫。事實上,即使我只是做users = req.body然後res.send(JSON.stringify(user)),它仍然是一個空數組 – orepor

+0

@ user1843115你嘗試過使用'setRequestHeader'嗎?至少應該解決在服務器端接收數據的問題。 – robertklep

+0

解決! 問題確實如我上面所述。我錯過了: request.setRequestHeader('content-type','application/json'); – orepor