服务器端的请求主体是一个空数组,表示node.js REST

时间:2013-05-28 06:55:16

标签: http rest post

所以我一直在寻找答案,似乎找不到一个

我正在创建一个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));

每当我查看Chrome时 - 我看到虽然我正在发送相关信息,但回复只是{}

这是我在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);

最后,这是Chrome测试时显示的内容 -

我忘记添加 - 这是请求在Chrome上的显示方式 -

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:oreporan@gmail.com, username:orepor, password:yotamp}
email: "john@gmail.com"
password: "password"
username: "john123"

请帮忙!

由于

1 个答案:

答案 0 :(得分:0)

我注意到了一些事情:

  • 即使您将客户端数据编码为JSON,您也不会告诉服务器它是您发送给它的JSON。在发送请求之前,请设置content-type:
   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)"
   }