如何在Ajax调用内的函数中获取返回值--JQuery

时间:2013-11-18 11:55:14

标签: javascript jquery ajax

对于你们这些人来说这听起来很容易,但是我无法弄清楚为什么我无法获得返回值,即使在收到很多帖子之后:(

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

现在我打电话给var count = getMessageCount(); 它让我没有瑕疵:( 而在方法内部计数正确,即服务工作正常。

5 个答案:

答案 0 :(得分:4)

我同意ahren的第一句话 '那是因为$ .ajax()调用是异步的。'

您可以尝试添加设置 - async:false 这默认是正确的。这使得呼叫同步。

您可以将代码编辑为:

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

答案 1 :(得分:3)

那是因为$.ajax()调用是异步的。

如果您将代码编辑为:

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

然后当你打电话时:

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

答案 2 :(得分:0)

使用回调:

调用函数如:

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

和另一个像:

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

}

答案 3 :(得分:0)

为什么你不把它传递给一个函数?

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

答案 4 :(得分:0)

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

现在,您将通过ajax调用从服务器端获取响应文本