1

I'll try to make this as broad as possible (I'm fairly new to working with AJAX) but I digress.

Currently, I have a function that calls different functions - so, getContents() calls getWeather(),getMovies(), etc.

In each of the said getXXXX() functions, there is an AJAX call to some php stuff - then I use a function to go through the data and take what I need - all fine and dandy.

However, only getWeather() executes - the following functions are never called. Below is a snippet of some of the relevant code, I believe the issue is with seperating the AJAX, but I can't be certain and searching hasn't provided me with anything too relevant.

I have tried performing what I need in one function to no avail - if I play with the order the first AJAX function works and the others are never fired.

function getContent(zipcode){
    getWeather(zipcode);
    getMovies(zipcode);
    getPlaces(zipcode);
    getMorePlaces(zipcode); 
}

Same as getmovies, etc

function getWeather(zipcode){
    $.ajax({ 
        url: '/dshbrd/php/weather.php',
        data: "zip_code=" + zipcode,
        dataType: "xml",
        type: 'POST',
        async: 'false',
        success: parseWeatherXml
    });
}

Same as parseMoviesXml,etc.

function parseWeatherXml(xml){
    $(xml).find("channel").each(function(){
        /*do stuff*/
    });
}

I realize this is more or less a 'newbie' question, my apologies if I'm using the wrong words/didn't actually search hard enough. Funny thing is, all the php files are executed on the Network (at least in Chrome) and they are returning relevant data - the issue seems to more or less involve my successes not being evaluated. Well, my first success is, anyway.

Edit: I also should mention, this is in no way the 'final' code - I do eventually plan to do everything with one call and the like, but this bug has certainly perplexed me at this prototype stage.

1 Answer 1

0

Ajax requests are asyncronous, thankfully they return an object that extends the Deferred object, so use something like:

function getWeather(zipcode){
    return $.ajax({ 
        url: '/dshbrd/php/weather.php',
        data: "zip_code=" + zipcode,
        dataType: "xml",
        type: 'POST',
        async: 'false',
        success: parseWeatherXml
    });
}

function getContent(zipcode){
    getWeather(zipcode).done(function () {
        getMovies(zipcode).done(function () {
            getPlaces(zipcode).done(function () {
                getMorePlaces(zipcode)
            });
        });
    }); 
}

Note that each getX function returns the result of the ajax request, so that you can use the .done of the Deferred object.

1
  • Ahh, that makes total sense. I had a feeling I had to wait. Thanks!
    – eric
    Feb 21, 2013 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.