javascript - Github page中为什么Ajax请求为混合内容(页面是https,请求为http)
本文介绍了javascript - Github page中为什么Ajax请求为混合内容(页面是https,请求为http)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
1.我在github pages里写了个静态页面,然后用AJAX获取同目录下img目录下的所有文件名,在本地apache(http)测试下完全ok了。但是挂到github pages上就提示访问混合内容。意思就是说github pages上覆盖了https,而我访问的是http。
2.AJAX代码
//异步获取文件信息
$.ajax({
url: getRootPath() + "/img",
success: function(data) {
//判断是否是图片
var regex = new RegExp("\\.jpg$|\\.png$");
var imgs = new Array();
var background = new Array();
$(data).find("a").each(function() {
var href = $(this).attr("href");
if (regex.test(href.toLowerCase())) {
var img = new Object();
var rank = parseInt(href.split(".")[0]);
//非数字命名的排到最后
img.rank = isNaN(rank) ? 10000 : rank;
img.src = getRootPath() + '/img/' + href
imgs.push(img);
}
});
//将数组按图片名称排序,按序插入到background
imgs.sort((img1, img2) => img1.rank - img2.rank);
imgs.map(img => background.push(img.src));
$.backstretch(background, {
fade: 2000
});
var index = 0;
$('#say').text(says[(index++) % says.length]);
setInterval(function() {
$.backstretch("next");
$('#say').hide();
$('#say').text(says[(index++) % says.length]);
$('#say').fadeToggle();
$('#carousel').carousel('next');
}, 6000);
}
});
function getRootPath() {
var curWwwPath = window.document.location.href;
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
var localhostPaht = curWwwPath.substring(0, pos);
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
return (localhostPaht + projectName);
}
这段代码我在Chrome控制台那边手动获取根路径是https开头的,然而它错误提示的却是http开头,请问下这是什么原因呢,要怎么修改呢? 万分感谢!!!
解决方案
...本地测试你的apache开了没有index.html就把文件夹目录作为列表现出。但是github pages可不会给的。。自己写个json文件吧把img下的图片都放进去。。
这篇关于javascript - Github page中为什么Ajax请求为混合内容(页面是https,请求为http)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文