2

this may sound very easy to few of you, but i am not able to figure out why I am not able to get the return value, even after chceking many posts :(

function getMessageCount() {
                    var count;
                    $.ajax({
                        type: "POST",
                        url: "http://localhost:43390" + "/services/DiscussionWidgetService.asmx/GetMessageCount",
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {                            
                            count = data.d;
                        } //success
                    });
                    return count;
                }

Now if I call var count = getMessageCount(); it gives me undefinted :( while inside the method count is coming correct, i.e. service is working fine.

7
  • 2
    You can't do that, jQuery.ajax is asynchronous. This question has been asked a lot, I'll look for a duplicate. Commented Nov 18, 2013 at 11:56
  • use count=JSON.parse(data).d Commented Nov 18, 2013 at 11:56
  • so,I cant get the value?? Commented Nov 18, 2013 at 11:56
  • @user2614405: you have to use a callback, not in the way that you're doing it, no. Commented Nov 18, 2013 at 11:56
  • 2
    possible duplicate of How to return the response from an AJAX call? Commented Nov 18, 2013 at 11:58

5 Answers 5

4

I agree with the first line by ahren that 'That's because the $.ajax() call is asynchronous.'

you could try adding a setting - async:false which is true by default. This makes the call synchronous.

you can edit your code as :

function getMessageCount() {
                var count;
                $.ajax({
                    type: "POST",
                    url: "http://localhost:43390" + "/services/DiscussionWidgetService.asmx/GetMessageCount",
                    dataType: "json",
                    async:false,
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {                            
                        count = data.d;
                    } //success
                });
                return count;
            }
Sign up to request clarification or add additional context in comments.

2 Comments

Worked perfectly for me :)
This is highly NOT recommended though as the user experience is horrible. Assume a connection is lost or dropped the ui thread is locked indefinitely. Never use async false for these sorts of things.
3

That's because the $.ajax() call is asynchronous.

If you edit your code to something like:

function getMessageCount(callback) {
    var count;
    $.ajax({
       type: "POST",
       url: "http://localhost:43390" + "/services/DiscussionWidgetService.asmx/GetMessageCount",
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (data) {                            
         count = data.d;

         if(typeof callback === "function") callback(count);
      } //success
   });
}

Then when you call it:

getMessageCount(function(count){
  console.log(count);
});

13 Comments

still getting undefined :( var noOfMessages; getMessageCount(function (count) { noOfMessages = count; });
When are you checking noOfMessages ? You should be checking within that callback.
@user2614405 - nope. The anonymous function within getMessageCount(...) won't return to where you think it will return. You'll need to place the rest of your code within the callback.
Then you'd need to pass it within the callback. Either way, you're going to need to wait for the result to come back from the $.ajax() call.
Because you're accessing that variable before it gets defined. Which is why you need to run the rest of your code within the callback.
|
0

use a callback:

call function like:

getMessageCount(function(result) {
//result is what you put in the callback. Count in your case
});

and the other like:

function getMessageCount(callback) {
    var count;
    $.ajax({
        type: "POST",
        url: "http://localhost:43390" + "/services/DiscussionWidgetService.asmx/GetMessageCount",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {                            
            count = data.d;
            if (callback) {
             callback(count);
            }
        } //success
    });

}

Comments

0

Why you don't just pass it to a function?

function getMessageCount() {
  var count;
  $.ajax({
    type: "POST",
    url: "http://localhost:43390" + "/services/DiscussionWidgetService.asmx/GetMessageCount",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) {                            
       count = data.d;
       countMessages(count);

    } //success
   });
 }

function countMessages(counted) {var counted = counted; console.log(counted);}

Comments

0
function ajax_call(url,data){
      return $.ajax({ type: "POST",
        url: url,
        data: data,
        async:false,
        success: function(status){
        }
    }).responseText;
}

Now you will get the response text from server side via ajax call

Comments

Your Answer

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