I'm using sails.js with Passport for user creation and sessions. I've created a user profile update screen with a submit button.
<form action="/user/update/<%= req.user[0].id %>" method="post" class="js-user-edit-form">
...
<button class="btn btn-primary btn-block" type="submit">Update</button>
</form>
...
<script type="text/javascript" src="/js/users.edit.js"></script>
This is the js file:
$(function() {
$('.js-user-edit-form').submit(function(event) {
event.preventDefault();
$.ajax({
type: "PUT",
url: $('.js-user-edit-form').attr('action'),
data: $('.js-user-edit-form').serialize()
})
.done(function(data) {
window.location.href = "/user";
})
.error(function() {
alert("Oops! Something went wrong.");
});
return false;
})
});
Relevant routes:
'get /user/edit': 'UserController.edit',
'get /user': 'UserController.index',
'get /user/:id': 'UserController.show',
User controller:
edit: function(req, res) {
User.findOne(req.user.id)
.done(function(err, user) {
return res.view({
user: user
});
});
},
My problem is that is that after i submit the update i get redirect to:
http://localhost:1337/user/update/53d033e685b861924ab55870
Rather than being redirected to
http://localhost:1337/user
When i use the console it outputs this:
Resource interpreted as Document but transferred with MIME type application/json: "http://localhost:1337/user/update/53d6207f40dee700004698e2".
As far as i can tell the javascript isn't working, because at the end of it i redirect to /user but that doesnt happen.