0

I get the images from a database and will add this to a array for the backstrech-Plugin.

This is the code I created:

/*
Get topslider
*/
function getHomeSlider()
{
var data = {};

var url = $( "#preloader" ).data( "url-slider" );
var imgArray = new Array();
$.ajax({
    type: "POST",
    url: url,
    data: data,
    dataType: "json"
})
.done(function(resp){
        $.each(resp, function(i, item) {
            imgArray.push( "http://192.168.1.30/attractionimages/" + item.folder + "/" + item.filename );
        });
        console.log(imgArray);
        return imgArray;
    })
.error(function(resp){

    });

}

$(".home-fullscreen-slider").backstretch(getHomeSlider(), {
fade: 750,
duration: 4000
});

When i make console.log of the imgArray, I got the following value:

["http://192.168.1.30/attractionimages/a23bz/DSC_0073.JPG", "http://192.168.1.30/attractionimages/a23bz/DSC_0135.JPG", "http://192.168.1.30/attractionimages/a23bz/DSC_0179.JPG", "http://192.168.1.30/attractionimages/a23bz/DSC_0273.JPG"]

Every Image is available, because, when i add this String instead of the function-call, then the images will be viewed. So the images are callable and available.

  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – showdev Dec 19 '14 at 01:05
  • the response from the ajax is correct, i got the values and i can fill the imgArray –  Dec 19 '14 at 01:08
  • 1
    Yes, but you are trying to return values from an asynchronous call. Since `$.ajax()` returns a promise, I suggest that you return that promise from your function and handle the results there. See "Use deferred objects / promises" in [this answer](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call#answer-14220323). – showdev Dec 19 '14 at 01:12
  • Thank you, i understand now my problem. its the asynchron. –  Dec 19 '14 at 01:24
  • 1
    For the sake of clarity, here's an example showing a method that doesn't work and a method that does work, based on that answer I linked: http://jsfiddle.net/kvwnvss1/. – showdev Dec 19 '14 at 01:24

0 Answers0