2013-05-21 73 views
0

我嘗試用express和backbone.js編寫寄存器。快遞部分看起來像express res.send http code and text catch up by backbone.js

app.post('/signup', function (req, res) { 

    var name = req.param("name", null); 
    var country = req.param("country", null); 
    var email = req.param("email", null); 
    var cemail = req.param("cemail", null); 
    var password = req.param("password", null); 

    if (email !== cemail || 
    !validate.Email(email) || 
    email == null) { 
     console.log("There is something wrong with email address"); 
     res.send(400, "Please check your email address."); 
     return; 
    }; 

    if (password == null || !validate.Password(password)) { 
     console.log("There is something wrong with password"); 
     res.send(400, "Password doesn't match security requirements"); 
     return; 
    }; 

    if (name == null || country == null) { 
     console.log("Some fields is not filled with value."); 
     res.send(400); 
     return; 
    }; 

    signup(name, country, email, password); 
    res.send(200); 

}); 

如果用戶提供無效的電子郵件地址,那麼它會響應HTTP代碼400和一些文本。現在我的問題是,我如何能夠在backbone.js網站上追上這篇文章。它有可能與否。 Backbone.js的前端代碼

$.post('/signup', { 
     name: $('input[name=name]').val(), 
     country: $('input[name=country]').val(), 
     email: $('input[name=email]').val(), 
     cemail: $('input[name=cemail]').val(), 
     password: $('input[name=password]').val() 
    }, function(data) { 
     console.log(data); 
    }).error(function(){ 
     console.log("Sign up error"); 
    }); 
    return false; 

回答

2

$.posterror回調將被傳入response對象,將包含文本錯誤消息。

.error(function(response){ 
    console.log("Sign up error", response.responseText); 
}); 
+0

首先這麼多的幫助,但你怎麼知道,我在看jQuery文檔,但沒有找到任何響應對象。 –

+0

http://api.jquery.com/jQuery.ajax/#jqXHR –