-1

I am not able to store a Json that I get through Ajax in Jquery in a global variable, it only works inside the function.

let ingJson;
let masasJson;
let salsasJson;

searchIngredients();
console.log(ingJson);

Here log throws me an undefined error when I need the Json returned to me so I can use it in the rest of the code.

function searchIngredients(){
    let origin = `/ingredientes.json`;
    $.get(origin).then(htmlIngredients);
}
function htmlIngredients(res){
    ingJson = res.ingredientes;
    masasJson = res.masas;
    salsasJson = res.salsas;
    console.log(ingJson);
}

Here log returns the Json correctly.

Jesus Esis
  • 15
  • 3

1 Answers1

-3

Initially assign to empty array,

var ingJson = [];

Then check length and do whatever stuff you need.