Setting local variable in a JavaScript callback function

后端 未结 5 1903
余生分开走
余生分开走 2021-01-02 11:33

I\'m relatively new to JavaScript and I thought I knew how callback functions worked but after a couple of hours of searching the web I still do not understand why my code i

5条回答
  •  天命终不由人
    2021-01-02 12:09

    Try calling a function to set this variable after your success:

    var array;
    
    var goodToProceed = function(myArr) {
       console.debug(myArr);
    };
    
    $.ajax({
    type: 'GET',
    url: 'include/load_array.php',
    dataType: 'json',
    success: function(data){
        goodToProceed(data);
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert("Error loading the data");
    }
    });
    

提交回复
热议问题