Question

I'm having an issue with the javascript below in which the $.ajax call is not being called. The alert('foo') does happen, but then the data call is completely skipped and the callback is never reached (never get alert('success!'). I don't completely understand callbacks, but this seems like it should be working.

Edit

Edited the script to where I currently stand, as I've read this way is better practice. Still, I can step in to authenticate(), it breaks on url:[...], but then never actually makes the ajax call. I've tried removing the return just to see if that's the problem, but it's producing the same result.

define(["jQuery", "kendo", "modernizr", "app/environment"], function ($, kendo, modernizr, environment) {
    var authenticate = function (username, password) {
        return $.ajax({
            url: environment.apiConnection + '/canlogin?userid=' + username + '&password=' + password,
            type: 'get',
            dataType: 'jsonp'
        });
    }

    var canLogin = function(data) {
        alert('good');
    }

    return {
        viewModel: kendo.observable({
            username: null,
            password: null,
            authenticate: function () {
                var username = this.get('username'),
                    password = this.get('password');

                authenticate(username, password).done(canLogin);
            }
        })
    }
});
Was it helpful?

Solution 2

The solution is a mixture of e.preventDefault() and using a callback in the ajax call. Just using the callback didn't resolve the issue, but adding preventDefault to the authenticate function and fixing the callback issue did.

The final, working version, now looks something like this:

define(["jQuery", "kendo", "modernizr", "app/environment"], function ($, kendo, modernizr, environment) {
    function authenticate(username, password, callback) {
        $.ajax({
            url: environment.apiConnection + '/canlogin?userid=' + username + '&password=' + password,
            type: 'get',
            dataType: 'jsonp',
            success: callback,
            error: function (x,t,r) {
                console.log('error')
            }
        });
    }

    function login(canLogin, username, password) {
        if (canLogin == false) {
            alert('Incorrect username or password');
            return;
        }

        alert('good');
    }

    return {
        viewModel: kendo.observable({
            username: null,
            password: null,
            authenticate: function (e) {
                e.preventDefault();
                var username = this.get('username'),
                    password = this.get('password');

                authenticate(username, password, function (canLogin) {
                    login(canLogin, username, password);
                });
            }
        })
    }
});

OTHER TIPS

Use a callback instead.

var canLogin = function (username, password, callback) {
    $.ajax({
        async: false,
        url: config.apiConnection + '/canlogin?userid=' + username + '&password=' + password,
        type: 'GET',
        dataType: 'jsonp',
        error: function (x, t, r) {
            alert('Error');
        },
        success: callback
    });

}

// use
canLogin("user","passwd",function( data ){
    alert("Im called on authentication success!");
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top