最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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 ing correct, i.e. service is working fine.

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 ing correct, i.e. service is working fine.

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

5 Answers 5

Reset to default 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;
            }

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

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

}

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

发布评论

评论列表(0)

  1. 暂无评论