【问题标题】:Second AJAX call data undefined第二个 AJAX 调用数据未定义
【发布时间】:2015-05-08 13:20:17
【问题描述】:

我有 2 个 ajax JSON 调用,第二个 URL 是从第一个传递的变量 nextURL

第二个 ajax 函数将 NextURL 变量注册为使用 Alert(nextURL) 进行测试,但我没有得到任何数据。错误控制台指出 $('#gameBoxleft').html(data.post.title); 数据未定义。

我不确定我在第二个 ajax 调用中是否做错了什么?

// -------------- 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);
    });
}

【问题讨论】:

  • 删除url: 'nextURL'nextURL 周围的单引号
  • 显示console.log(data) ???请
  • 你只是让我捂脸,我没有在 .done(function) 中传递数据
  • 问题已被编辑,现在看到答案时有点混乱......
  • 抱歉,将恢复原状。

标签: jquery ajax


【解决方案1】:

您在' ' 中提到了nextURL,因为它应该是您从调用函数发送的参数,如下所示:

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);
    });
}

【讨论】:

    【解决方案2】:

    我认为你应该通过:

    url: nextURL,
    

    代替:

    url: 'nextURL',
    

    【讨论】:

      【解决方案3】:

      在您的第二个 ajax 中,您在变量中传递 url:

      这个变量是:nextURL

      从变量名中删除配额标记:

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

      在这种情况下,变量的值将作为 URL,否则,url: 'nextURL' 中的变量内部有配额标记,URL 将被视为 nextURL,这不是 URL。而且您还没有处理错误,因此它不会给出任何错误。

      您可以在浏览器控制台中检查错误。

      【讨论】:

        【解决方案4】:

        掌心时刻....

        .done(function(data) 会有所帮助!

        【讨论】:

          猜你喜欢
          • 2017-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-16
          • 2023-04-03
          • 1970-01-01
          • 2015-12-12
          相关资源
          最近更新 更多