于是找遍了答案,好像没找到
我正在制作一个 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"
请帮忙!
谢谢