1

I am complete beginner to Ember and jQuery. I am trying to implement a login page and I can't understand what should I return from the server side for the case of failed login. The code I have written until now is:

App.LoginController = Ember.Controller.extend({
    loginFailed: false,

    login: function() {
        this.setProperties({
            loginFailed: false
        });

        var request = $.post("/Login", this.getProperties("username", "password"));
        request.then(this.success.bind(this), this.failure.bind(this));
    },

    success: function() {
        // sign in logic
        alert("Successful Login");  
    },

    failure: function() {
        this.set("loginFailed", true);
    }
});

I have a login template and username and password are bound to inputs in the template. Currently, I am able to get the requests on the server side but I can't understand what should I return from server side for case of success and failure.

Scott
  • 21,211
  • 8
  • 65
  • 72
Pranava Sheoran
  • 529
  • 4
  • 27
  • You can return basic boolean variable or if you want message from server side you can send a json with {messagecode: message} it all depends on what is convenient for you – Raunak Kathuria Dec 24 '13 at 01:59

1 Answers1

1

You will want to just spit out some sort of output. What language are you running on the server? In php I would do something like this

while(ob_end_clean()); // make sure my output buffer is clean
// This is your object that will be returned to the client
$json_out = array(
  'status' => 0 // status 0 equals failed login, 1 will mean success
  'message' => '' // if you would like to display a message and have the server control it
);
if($successful_login){
  $json_out['status'] = 1;
}
header('Content-Type: application/json');
echo json_encode($json_out);
exit;
user2923779
  • 230
  • 1
  • 4
  • Thanks for your reply. Actually the server side is written in Java. So if I return a JSON object, with status:0, the client side code would automatically interpret it as failure? – Pranava Sheoran Dec 24 '13 at 02:03
  • no, your success callback will take an argument that will hold the JSON object/output from the server. This might help with what I am talking about http://stackoverflow.com/questions/17559773/ember-js-rest-ajax-success-and-error – user2923779 Dec 24 '13 at 02:05
  • I think I finally get it. The code would always enter the success callback if the request goes through. So even in the case of a failed login, I have to return a JSON object and inside the success block I have to decide what to do with failed login. The code will go to the failure block only if the AJAX request fails. – Pranava Sheoran Dec 24 '13 at 02:27
  • Exactly - it will enter success block based on the HTTP response code of server (error block if not 20x). – user2923779 Dec 24 '13 at 02:28