Reputation: 3625
I'm working on my first major project using Backbone. I've got a Backbone view called SignUpView
with a method as follows:
register: function(){
this.model.save({
email: $('#email').val(),
password: $('#passwd').val()
},{
success: function(model, response){
console.log("Registration succeeded, congrats!");
window.location.hash = '';
},
error: function(model, response){
console.log("Registration failed, reason = "
+ response.responseText
+ " (status "+response.status+")");
}
});
return false;
}
However, whenever I run it, only the error
callback gets called. This leads to strange behavior such as the console logging
Registration failed, reason = Registration succeeded (status 200)
("Registration succeeded" is sent back from the server upon successful registration, otherwise it would say "Missing parameter 'password'" or "Invalid email format" or similar.)
I know I could simply catch the 200 status code in the error block, but I don't like the fact that I'd then have to be handling a successful post in a function labeled "error". I know it's probably something really simple and stupid, but I just can't find what's wrong.
Upvotes: 4
Views: 1068
Reputation: 3625
The problem, according to this issue, is that invalid JSON is also considered an error. If the server returns invalid JSON (such as Registration succeeded
), then this will be handled in the error handler.
I changed my server response to be
{"ok":"Registration succeeded"}
and everything started working as expected.
Upvotes: 5