I am trying to send the data inserted into form to server via ajax request like this:
handleUserSubmit: function(user) {
var users = this.state.data;
user.id = Date.now();
var newUsers = users.concat([user]);
this.setState({data: newUsers});
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: user,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
this.setState({data: users});
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
and the rendering is done as:
ReactDOM.render(
<UserBox url="../users" pollInterval={2000} />,
document.getElementById('data')
);
I am getting this weird console error:
POST http://localhost:3000/users 404 (Not Found)
Since the "users" file (assuming it's trying to access users.json file is in a directory up one level). I have tried all variation possible : "./users", "../users", "#/users" etc...
The code error on this line:
url: this.props.url,
in the code given above. Any idea what's going on?
My node.js code is:
app.post('/users', function(req, res) {
fs.readFile(USERDATA, function(err, data) {
if (err) {
console.error(err);
process.exit(1);
}
var users = JSON.parse(data);
var newUser = {
id: Date.now(),
firstname: req.body.firstname,
lastname: req.body.lastname,
address: req.body.address,
phonenumber: req.body.phonenumber,
email: req.body.email,
licensestate: req.body.licensestate,
licensenum: req.body.licensenum
};
console.log(newUser)
users.push(newUser);
fs.writeFile(USERDATA, JSON.stringify(users, null, 10),
function(err) {
if (err) {
console.error(err);
process.exit(1);
}
res.json(users);
});
});
});