Pranava Sheoran
Pranava Sheoran

Reputation: 529

What to return from server side for success to ajax call in Ember.js

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.

Upvotes: 1

Views: 824

Answers (1)

user2923779
user2923779

Reputation: 230

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;

Upvotes: 1

Related Questions