$(document).ready(function(){
$.ajax({
url: 'https://swapi.co/api/people/1/?format=json',
type:'GET',
dataType: 'JSON',
success: function(json){
console.log(json.name);
$("h2.name").text(json.name);
}
});
const ITERACIONES_1=6;
const ITERACIONES_2=3;
let helper= {
film:'',
counter:0,
method: function (per,res) {
this.film += res.title;
this.counter++;
if (this.counter == (ITERACIONES_1 * ITERACIONES_2)) {
for (var index = 0; index < ITERACIONES_1; index++) {
var a = $("<div data-toggle='modal' data-target='#myModal'" +
"onclick=\"data2('"+this.film+"')\" class='col-md-4'>" +
per.results[index].name + "</div>");
$(".row").append(a);
console.log(per.results[index].name);
}
$(".btnNext").attr("id", per.next);
}
}
};
$.ajax({
url: 'https://swapi.co/api/people/?format=json',
type:'GET',
dataType: 'JSON',
success: function(per){
$(".row").empty();
var film;
for (var i = 0; i < ITERACIONES_1; i++) {
for (var index = 0; index < ITERACIONES_2; index++) {
var ul = per.results[i].films[index];
$.ajax({
url:ul,
type: 'GET',
dataType: 'JSON',
success:function(res){
helper.method(per,res);
}
});
}
}
}
});
$(".btnNext").click(function(){
var a = $(".btnNext").attr("id");
$.ajax({
url: a,
type:'GET',
dataType: 'JSON',
success: function(per){
var film;
for (var i = 0; i < ITERACIONES_1; i++) {
for (var index = 0; index < ITERACIONES_2; index++) {
var ul = per.results[i].films[index];
$.ajax({
url:ul,
type: 'GET',
dataType: 'JSON',
success:function(res){
helper.method(per,res);
}
});
}
}
}
});
});
});
function data2(nom){
//alert(nom);
$(".modal-body > p").remove();
$(".modal-body").append("<p>"+nom+"</p>");
}
执行点击事件时,我收到此错误
jquery.min.js:4 无法加载文件:///C:/Users/Emma/Desktop/test/starwars.html#:跨源请求仅支持协议方案:http、data、chrome、chrome-extension ,https。
原则上,它告诉您的是您正在尝试访问“外部”服务器上的文件。
在一个页面上,我阅读了几种“绕过”该限制的方法,我把它放在这里,你看看。
使用 Ajax 打开本地页面
在其中,他谈到了两种方法,它们是:
关闭所有 Chrome 页面(如果没有,它不起作用),打开命令控制台,并写入此路径,在 Windows 的情况下为“C:\Program Files (x86)\Google\Chrome\Application\chrome. exe" --allow-file-access-from-files。根据您拥有的 Windows 版本,您需要从名称中删除 (x86)。完成此操作后,您将能够运行它。当您关闭窗口时,您将不得不再次重复这些步骤。
使用 Internet Explorer(不推荐)。
我希望这个对你有用。