我正在尝试向Parse.com上托管的Express JS服务器发送POST调用。我发送的数据如下:
var data = new Array();
data["firstName"] = firstName;
data["lastName"] = lastName;
data["dateOfBirth"] = dateOfBirth;
data["mobileNumbe"] = mobileNumber;
data["email"] = email;
data["reEmail"] = reEmail;
data["pw"] = pw;
data["rePw"] = rePw;
$.ajax({
url: "/api/post/registerclient",
type: "POST",
dataType: "application/json",
data: { data:data
},
success: function(result){
disabledButton("buttonRegister",false);
if(data.success == true){
console.log("Neu registriert");
}else{
showErrorMessageWithError(result.error);
}
},
error:function(error){
disabledButton("buttonRegister",false);
var string = "Es ist ein Fehler aufgetreten!"
showErrorMessageWithError(string);
}
});
在服务器端,我尝试使用req.body.data捕获数据,但它是空的:
exports.registerClient = function(req,res){
//Data, get = firstName, lastName, dateOfBirth, mobileNumber, email, reEmail, pw, rePw
//Data, respond = success, error
var validation = helper.validateRegisterClient(req.body.data);
if(validation.length == 0){
var user = new Parse.User();
user.set("username", req.data.email);
user.set("password", req.data.pw);
user.set("email", req.data.email);
//optional fields
user.set("mobileNumber", req.data.mobileNumber);
user.set("firstName", req.data.firstName);
user.set("lastName", req.data.lastName);
user.set("dateOfBirth", req.data.dateOfBirth);
user.signUp(null, {
success: function(user) {
// Hooray! Let them use the app now.
var success = true;
var error = "";
var json = {success: success, error: error};
res.json(json);
},
error: function(user, error) {
var success = false;
var json = {success: success, error: error};
res.json(json);
}
});
}else{
var success = false;
var error = validation;
var json = {sucess: success, error: error};
res.json(json);
}
}
我感觉,我的快递应用程序可能配置错误,所以这是我的" init":
var express = require('express');
var parseExpressHttpsRedirect = require('parse-express-https-redirect');
var parseExpressCookieSession = require('parse-express-cookie-session');
var app = express();
// Global app configuration section
app.set('views', 'cloud/views'); // Specify the folder to find templates
app.set('view engine', 'ejs'); // Set the template engine
app.use(parseExpressHttpsRedirect());
app.use(express.bodyParser()); // Middleware for reading request body
app.use(express.cookieParser('xxxxxxxxxxxxxxxxxxxx'));
app.use(parseExpressCookieSession({ cookie: { maxAge: 3600000 } }));
答案 0 :(得分:1)
你的快递代码中有一个拼写错误。您应该req.data
时引用req.body.data
:
例如,这个:
var user = new Parse.User();
user.set("username", req.data.email);
user.set("password", req.data.pw);
user.set("email", req.data.email);
应该是这样的:
var user = new Parse.User();
user.set("username", req.body.data.email);
user.set("password", req.body.data.pw);
user.set("email", req.body.data.email);
此外,您的初始数据数组实际上是一个对象,应该以不同方式实例化。所以这个:
var data = new Array();
应该是这样的:
var data = {};
我刚才注意到的另一件事是你在ajax成功回调中引用了data.success
。 data.success
在此范围内不存在。这应该是result.success
。
所以这个:
if(data.success == true){
console.log("Neu registriert");
}else{
showErrorMessageWithError(result.error);
}
应该是:
if(result.success == true){
console.log("Neu registriert");
}else{
showErrorMessageWithError(result.error);
}