How to get return value in a function with inside Ajax call - JQuery

user2614405

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.

ahren

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get a value inside a clickHandler function jQuery

From Dev

jquery function plugin with return value like $.get in ajax

From Dev

Jquery ajax call inside success function

From Dev

How to get address of function return value inside constructor via pointer

From Dev

return the value from a promise/ajax call with jquery

From Dev

jQuery deferred AJAX call return value

From Dev

Java debug mode: how to get return value of function call

From Dev

how to catch the jquery ajax function return value in another variable

From Dev

get return value from php for ajax call

From Dev

jquery ajax call returns object, how to get object value

From Dev

jquery ajax call returns object, how to get object value

From Dev

how to call autocomplete inside ajax function

From Dev

How to call medoo 1.5 inside a function with ajax?

From Dev

Get return value inside a function with javascript

From Dev

ajax call inside ajax success jquery then ajaxstart function not working

From Dev

how to take value return by ajax call?

From Dev

how to get a value inside a function

From Dev

get return value of jQuery get().done() function

From Dev

Jquery Ajax Call Return

From Dev

How to pass a jquery variable as a url parameter inside an ajax call of a fancybox function

From Dev

How does a defined variable get set to undefined inside an ajax call via jQuery?

From Dev

Call function inside CodeIgniter's controller using jquery / ajax

From Dev

How I can call javascript function and get the return value from javascript function

From Dev

How to call stored procedure within function by output value, then get and return this value?

From Dev

Return value inside normal function that has Jquery function

From Dev

How to call PHP function using jQuery ajax?

From Dev

how to determine jquery ajax return value or null

From Dev

get ajax returned value in a jquery function

From Dev

how to return two maps value inside return function - react js

Related Related

  1. 1

    How to get a value inside a clickHandler function jQuery

  2. 2

    jquery function plugin with return value like $.get in ajax

  3. 3

    Jquery ajax call inside success function

  4. 4

    How to get address of function return value inside constructor via pointer

  5. 5

    return the value from a promise/ajax call with jquery

  6. 6

    jQuery deferred AJAX call return value

  7. 7

    Java debug mode: how to get return value of function call

  8. 8

    how to catch the jquery ajax function return value in another variable

  9. 9

    get return value from php for ajax call

  10. 10

    jquery ajax call returns object, how to get object value

  11. 11

    jquery ajax call returns object, how to get object value

  12. 12

    how to call autocomplete inside ajax function

  13. 13

    How to call medoo 1.5 inside a function with ajax?

  14. 14

    Get return value inside a function with javascript

  15. 15

    ajax call inside ajax success jquery then ajaxstart function not working

  16. 16

    how to take value return by ajax call?

  17. 17

    how to get a value inside a function

  18. 18

    get return value of jQuery get().done() function

  19. 19

    Jquery Ajax Call Return

  20. 20

    How to pass a jquery variable as a url parameter inside an ajax call of a fancybox function

  21. 21

    How does a defined variable get set to undefined inside an ajax call via jQuery?

  22. 22

    Call function inside CodeIgniter's controller using jquery / ajax

  23. 23

    How I can call javascript function and get the return value from javascript function

  24. 24

    How to call stored procedure within function by output value, then get and return this value?

  25. 25

    Return value inside normal function that has Jquery function

  26. 26

    How to call PHP function using jQuery ajax?

  27. 27

    how to determine jquery ajax return value or null

  28. 28

    get ajax returned value in a jquery function

  29. 29

    how to return two maps value inside return function - react js

HotTag

Archive