Here is the view i am working on. It is a simple registration form. It captures data from a group of input tags, and posts it to the server. The POST data does not seem to be making it to the server however.
define(['text!templates/register.html'], function(registerTemplate){
var registerView = Backbone.View.extend({
el: $('#content'),
events: {
"submit form" : "register"
},
register: function() {
var postData = {
firstname: $('input[name=firstname]').val(),
lastname: $('input[name=lastname]').val(),
email: $('input[name=email]').val(),
password: $('input[name=password]').val()
};
$.post('/register', postData, function(data){
console.log(data);
}
}).error(function(err){
console.log(err);
});
return false;
},
render: function(){
this.$el.html(registerTemplate);
}
});
return registerView;
});
Here is the request listener in my app. All the variables show 'undefined' when logged.
app.post('/register', function(req, res){
var firstname = req.body.firstname,
lastname = req.body.lastname,
email = req.body.email,
password = req.body.password;
console.log(email);
console.log(firstname);
console.log(lastname);
console.log(password);
if (email === null || password === null) {
res.send(400);
} else {
Account.register(email, password, firstname, lastname);
res.send(200);
}
});
I can see that the listener in my app is being triggered when the form is submitted, but it is not getting the POST data i sent. In the browser console all i get for an error is "POST htp://localhost:3000/register 500 (Internal Server Error)"
This worked. I found the data in req.param.
app.post('/register', function(req, res){
var firstname = req.param('firstname'),
lastname = req.param('lastname'),
email = req.param('email'),
password = req.param('password');
if (email === null || password === null) {
res.send(400);
} else {
Account.register(email, password, firstname, lastname);
res.send(200);
}
});
if you are familiar with http, you know 500 it's a status that server responses to client, it tells client there is a problem when i dealing with the data you posted. so focus on your server-side codes.mostly, there are some logical problem in your server codes.