0

Pretty basic question but I'm such newb so can someone clear this for me?

I have a global function like this:

function.js

 function getCampaigns() {
        $.ajax({
            type: "GET",
            url: SERVER_URL + "campaigns",
            headers: {
                "authorization": localStorage.getItem("token")
            },
            contentType: "application/json; charset=utf-8",
            crossDomain: true,
            dataType: "json",
            success: function (data, status, jqXHR) {
                if (data) {
                    var campaignArray = [];
                    _.each(data, function (campaign) {
                        campaignArray.push(campaign);
                    });
                    console.log(campaignArray);
                    return campaignArray;
                } else {
                    console.log("error");
                }
            },

            error: function (jqXHR, status) {
                // error handler
                console.log(jqXHR);
                alert('fail' + status.code);
            }
        });
    }

The console log works fine and displays correct result but i want to return the array to be used where i call it. When i try to call it in another file it says "undefined"

another.js

var test = getCampaigns();
console.log(test);
Jack M.
  • 1,831
  • 5
  • 24
  • 36

0 Answers0