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

后端 未结 5 1207
遥遥无期
遥遥无期 2021-01-07 02:39

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         


        
相关标签:
5条回答
  • 2021-01-07 03:28

    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;
                }
    
    0 讨论(0)
  • 2021-01-07 03:32

    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);
    });
    
    0 讨论(0)
  • 2021-01-07 03:37

    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
        });
    
    }
    
    0 讨论(0)
  • 2021-01-07 03:38
    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 讨论(0)
  • 2021-01-07 03:41

    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);}
    
    0 讨论(0)
提交回复
热议问题