1

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??

2
  • are you sure your response is an object? what is the output of console? Commented Mar 25, 2014 at 7:42
  • 'true' or 'false' depends Commented Mar 25, 2014 at 7:44

4 Answers 4

1

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
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Typo

Checkusers -with s

CheckUser() -without s

2 Comments

come on it was mistake in question posting!! In real code both are same!!
No worries. I ain't downvoting or nothing. Everyone can make a mistake :)
0

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

Comments

0

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
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.