Variable doesn't get returned from AJAX function

后端 未结 2 741
南笙
南笙 2020-11-22 04:12

As my framework grows i decided to split it into files, instead of leaving it in the main design file. However by doing that the return of a function doesn\'t return any val

相关标签:
2条回答
  • 2020-11-22 04:24

    You can't do that : as the call is asynchronous, the get_data function can't return the result of the ajax call.

    What you should do is provide a callback to the get_data function and handle the result in the callback.

    function get_data(data, destination, callback) 
             {
    
                if (lock_get == 0)
                {
                    lock_get = 1;
                    $.ajax({
                        type: "POST",
                        url: destination,
                        async: true,
                        data: data,
                        success: function(data) 
                        {
                            lock_get = 0;
                            if (data && callback)
                            {
                                callback(data);
                            }
                        }
                    });
                }
             };
    

    And call it like that :

    get_data(data, destination, function(test){
       notice(test);
    });
    
    0 讨论(0)
  • 2020-11-22 04:38

    Alternatively you can return from synchronous AJAX call, it's generally BAD idea to use synchronous calls, but it may be useful sometimes:

    http://pratyush-chandra.blogspot.com/2012/04/store-ajax-json-response-into.html

    0 讨论(0)
提交回复
热议问题