user3452210
user3452210

Reputation: 147

jquery ajax Function undefined

Returning a response from a function every time end up as undefined function!!

var result = Checkusers();(result is undefined) 

function CheckUser() {

    var EmpName = $("#txtName").val();

    $.ajax({        
        type: "POST",
        url: location.pathname + "/UserExist",
        data: "{Name:'" + EmpName + "'}",
        contentType: "application/json; charset=utf-8",
        datatype: "jsondata",
        async: "true",
        success: function (response) {
            console.log(response.d);
            var obj = eval('(' + response.d + ')');
            return obj;
        },
        error: function (response) {
            alert(response.status + ' ' + response.statusText);         
        }
    });
}

i am calling this function as

var Result = CheckUser();
if(Result== false){ 
    //do something
} else{
    //do something
}

I have struggling with this from past one day!! I read in a section that it is because of 'Ajax is Asynchronous' . But how could i handle it??

Upvotes: 1

Views: 1132

Answers (4)

s4m0k
s4m0k

Reputation: 568

function CheckUser() doesn't have any return statement that is why your result is always undefined.

Pass a callback instead to your function and invoke it upon success or error of your ajax call.

something like this :

function CheckUser(callback) {

var EmpName = $("#txtName").val();

$.ajax({        
    type: "POST",
    url: location.pathname + "/UserExist",
    data: "{Name:'" + EmpName + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: "true",
    success: function (response) {
        console.log(response.d);
        var obj = eval('(' + response.d + ')');
        callback(null, obj);
    },
    error: function (response) {
        alert(response.status + ' ' + response.statusText);
        callback(response);         
    }
});
}

then :

var Result = CheckUser(function ( err, data) {
    // check if no err then process whatever data format you have
});

Upvotes: 0

Szilard Muzsi
Szilard Muzsi

Reputation: 1891

You're better off passing a callback function to the CheckUser

function CheckUser(callback) {

    var EmpName = $("#txtName").val();

    $.ajax({        
        type: "POST",
        url: location.pathname + "/UserExist",
        data: "{Name:'" + EmpName + "'}",
        contentType: "application/json; charset=utf-8",
        datatype: "jsondata",
        async: "true",
        success: function (response) {
            console.log(response.d);
            var obj = eval('(' + response.d + ')');
            callback(obj);
        },
        error: function (response) {
            alert(response.status + ' ' + response.statusText);
            callback(null);         
        }
    });
}

You would then call this function as so

CheckUser(function (res) {
    if (res === null) {
       //false
    } else {
       //true
    }
});

Upvotes: 1

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

change

datatype:'jsondata'

to

 dataType: "json"

Since the ajax is asynchronous you cant return data like that. You need to write the things on success handler of ajax

$.ajax({
type: "POST",
url: location.pathname + "/UserExist",
data: "{Name:'" + EmpName + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: "true",
success: function (response) {
   //do your stuff here

},
error: function (response) {
    alert(response.status + ' ' + response.statusText);
}
});

If you want to return data from ajax, you should use async : false for that

function CheckUser() {
var EmpName = $("#txtName").val();
var CheckUser;
$.ajax({
    type: "POST",
    url: location.pathname + "/UserExist",
    data: "{Name:'" + EmpName + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false,
    // ...
    success: function (jsonData) {
        CheckUser = jsonData
    }
});
return CheckUser
}

but its not a good approach, it will freeze the browser

Upvotes: 0

Joakim M
Joakim M

Reputation: 1803

Typo

Checkusers -with s

CheckUser() -without s

Upvotes: 0

Related Questions