I am having trouble using a JSON object outside of the AJAX success scope. I need to access the returned JSON object outside the AJAX success scope. I tried initializing a JS var outside the AJAX scope however and the assigning the JSON to it. However, this approach results in a catalog.item is undefined
error. I appreciate any suggestions that will help me fix this problem.
This is what I tried:
This approach works perfectly fine (but not what I need):
$('.catViewButton').click(function(){
var overlay = jQuery('<div class="overlay"> </div>');
overlay.appendTo(".invoice-body");
$('.catalog-view-wrapper').show();
$.ajax({
url: "ajax/invoice-get-data.php?loadCatalog=1",
dataType: "json",
success: function(catalog){
alert(catalog.item[0].image);
$('.catalog-img-container').attr("src", catalog.item[0].image);
}
});
...more code
.....
.....
This approach is what I need but results in an error:
var catalog = [];
$('.catViewButton').click(function(){
var overlay = jQuery('<div class="overlay"> </div>');
overlay.appendTo(".invoice-body");
$('.catalog-view-wrapper').show();
$.ajax({
url: "ajax/invoice-get-data.php?loadCatalog=1",
dataType: "json",
success: function(cat){
catalog = cat;
}
});
alert(catalog.item[0].image);
$('.catalog-img-container').attr("src", catalog.item[0].image);
...more code
.....
.....
Many thanks in advance!