繁体   English   中英

Ajax请求中的jQuery Ajax请求不起作用

[英]jQuery Ajax request inside Ajax request not working

编辑:这是在告诉我我的li是非法令牌。 仍然不确定该怎么做,因为我以为我要追加到ul中

因此,我必须获取一些公共Github存储库的JSON数组,并将每个存储库的前10个提交中的标题,链接,描述和基本信息附加到页面上。 我已经可以使用标题,链接和描述了,但是当涉及到提交时我迷路了。 它们位于与初始信息不同的地址,因此我在第一个ajax请求中向提交地址发出了第二个ajax请求,但没有任何反应。 可能只是语法错误,或者是我在广泛搜索中发现的可能嵌套ajax请求的问题。

这是我的代码,分成了每个单独的函数(出于匿名的原因,我用“ name”替换了组织名称):

主功能:

function main() {
  $.ajax({
    type: 'GET',
    url: 'https://api.github.com/orgs/name/repos',
    error: function(xhr, status, error){
      //var err = eval("(" + xhr.responseText + ")");
      var err = JSON.parse(xhr.responseText);
      alert(err.Message);
    },
    complete: function(data){
      display_content(data);
    }
  });
}
main();

内容显示功能:

function display_content(_data){
  for (var i = 0; i < _data.responseJSON.length; i++) {
    $("#listing").append(
      "<h2>" +
        "<a href=\"" + _data.responseJSON[i].html_url + "\">" + _data.responseJSON[i].full_name + "</a></h2>" +
      "<p>" + _data.responseJSON[i].description +
        "<h3>Latest Commits</h3>" + 
        "<ul id= \"" + _data.responseJSON[i].name + "commits\">"
    );

    commits(_data.responseJSON[i].name);

    $('#listing').append(
        "</ul>" +
      "</p>" +
      "<br/>"
    );
  }
}

提交信息功能:

function commits(repo){
  $('#listing').find('h3').text("Latest Commits to user/" + repo);
  return $.ajax({
    type: 'GET',
    url: 'https://api.github.com/repos/user/' + repo + '/commits',
    error: function(xhr, status, error){
      //var err = eval("(" + xhr.responseText + ")");
      var err = JSON.parse(xhr.responseText);
      alert(err.Message);
    },
    complete: function(commit_data){
      for(var i = 0; i < 10; i++){
        $('#listing').append(
          "<li>
            <div>
              <img src=\"" + commit_data.responseJSON[i].author.avatar_url + "\"> 
              <a href=\"https://github.com/" + commit_data.responseJSON[i].author.login + "\">
                <b>" + commit_data.responseJSON[i].author.login + "</b>
              </a>
              <b>" + ($.timeago(commit_data.responseJSON[i].commit.committer.date)) + "</b><br />
              <i>SHA: " + commit_data.responseJSON[i].sha + "</i>
            </div>
            <a href=\"https://github.com/user/" + repo + "/commit/" + commit_data.responseJSON[i].sha + 
              "\" target=\"_blank\">" + commit_data.responseJSON[i].commit.message +
            "</a>
          </li>"
        );
      }
    }
  });
}

我知道很多代码,我事先表示歉意。 我知道问题出在最后两个函数中,但我想确保所有内容都包括在内。 如果有人对获取和解析Git回购信息有任何经验,我将非常感谢您提供任何建议。 我愿意放弃一切并重新开始,如果需要的话,但是我想避免使用除jQuery之外的任何库。

谢谢任何帮助! 我很感激。

告诉我我的李是非法令牌。 仍然不确定该怎么做,因为我以为我要追加到ul中

您正在使用多行字符串文字添加html。 需要避免换行,可以通过反斜杠来实现。

改成

function commits(repo){
  $('#listing').find('h3').text("Latest Commits to user/" + repo);
  return $.ajax({
    type: 'GET',
    url: 'https://api.github.com/repos/user/' + repo + '/commits',
    error: function(xhr, status, error){
      //var err = eval("(" + xhr.responseText + ")");
      var err = JSON.parse(xhr.responseText);
      alert(err.Message);
    },
    complete: function(commit_data){
      for(var i = 0; i < 10; i++){
        $('#listing').append(
          "<li>"+
            "<div>"+
              "<img src=\"" + commit_data.responseJSON[i].author.avatar_url + "\">"+
              "<a href=\"https://github.com/" + commit_data.responseJSON[i].author.login + "\">"+
                "<b>" + commit_data.responseJSON[i].author.login + "</b>"+
              "</a>"+
              "<b>" + ($.timeago(commit_data.responseJSON[i].commit.committer.date)) + "</b><br />"+
              "<i>SHA: " + commit_data.responseJSON[i].sha + "</i>"+
            "</div>"+
            "<a href=\"https://github.com/user/" + repo + "/commit/" + commit_data.responseJSON[i].sha + 
              "\" target=\"_blank\">" + commit_data.responseJSON[i].commit.message +
            "</a>"+
          "</li>"
        );
      }
    }
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM