gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:53:45 25 4
gpt4 key购买 nike

编辑:它告诉我我的 li 是非法 token 。仍然不知道该怎么办,因为我以为我是在 ul 中附加

因此,我必须获取一些公共(public) Github 存储库的 JSON 数组,并将每个存储库前 10 次提交的标题、链接、描述和基本信息附加到页面。我已经有了标题、链接和描述,但在引入提交时我迷失了方向。它们位于与初始信息不同的地址,因此我在第一个 ajax 请求中向提交地址发出了第二个 ajax 请求,但没有任何效果。这可能只是一个语法错误,或者我通过大量搜索发现可以嵌套 ajax 请求。

这是我的代码,分为每个单独的函数(为了匿名,我已将组织名称替换为“名称”):

主要功能:

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 之外的任何库。

感谢所有提供帮助的人!我很感激。

最佳答案

It's telling me that my li is an illegal token. Still not sure what to do about this as I thought I was appending inside a 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>"
);
}
}
});
}

关于javascript - Ajax 请求中的 jQuery Ajax 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26400145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com