user4831663
user4831663

Reputation:

Second AJAX call data undefined

I have 2 ajax JSON calls with the second URL being a variable nextURL passed from the first.

The second ajax function registers the NextURL variable as tested with an Alert(nextURL) but I do not got any data. Error console states that $('#gameBoxleft').html(data.post.title); data is undefined.

I'm not sure if I have done something wrong with the second ajax call?

// -------------- MAIN AJAX CALL FUNCTION  --------------
function call_ajax(url, elem) {

    $.ajax({
        url: url,
        method: "GET",
        data: {json: 1},
        dataType: "JSON"
    })


    // -------------- FUNCTIONS FOR AFTER AJAX DONE --------------
    .done(function (data) {

        // Append the box
        appendBox(elem);

        // LOAD GAMEBOX JSON DATA

        $("#game-name").html(data.post.title);
        $("#game-reels").html(data.post.custom_fields.reels);
        $("#game-paylines").html(data.post.custom_fields.paylines);
        $("#game-minBet").html(data.post.custom_fields.min_bet);
        $("#game-maxBet").html(data.post.custom_fields.max_bet);
        $("#game-jackpot").html(data.post.custom_fields.jackpot);
        $("#game-info").html(data.post.custom_fields.game_info);


    var nextURL = (data.previous_url) + "?json=1";
            var prevURL = (data.next_url);

          processTwo(nextURL);

    });
}


// -------------- NEXT OBJEXT AJAX CALL FUNCTION  --------------
function processTwo(nextURL) {

alert(nextURL);
            $.ajax({
        url: 'nextURL',
        method: "GET",
        data: {json: 1},
        dataType: "JSON"
    })

            .done(function() {

          $('#gameBoxleft').html(data.post.title);
    });
}

Upvotes: 1

Views: 228

Answers (4)

user4831663
user4831663

Reputation:

Face palm moment....

.done(function(data) would help!

Upvotes: 0

FatalError
FatalError

Reputation: 550

In your second ajax you are passing the url in a varaible:

This variable is: nextURL.

Remove quota mark from the variable name:

$.ajax({
    url: nextURL, //remove quota marks.
    method: "GET",
    data: {json: 1},
    dataType: "JSON"
})

In this case the value of variable will be as URL otherwise, with quota marks inside variable in url: 'nextURL', the URL will treat as nextURL, which is not a URL. And you have not handle the error so it will not give any error.

You can check the error in your browsers console.

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 32831

I think you should pass:

url: nextURL,

instead of:

url: 'nextURL',

Upvotes: 0

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29693

You have mentioned nextURL inside ' ' where as it should be the parameter you are sending from the calling function as below:

function processTwo(nextURL) {
    alert(nextURL);
    $.ajax({
        url: nextURL, //This needs to be changed
        method: "GET",
        data: {json: 1},
        dataType: "JSON"
    })
    .done(function() {
         $('#gameBoxleft').html(data.post.title);
    });
}

Upvotes: 1

Related Questions