阿贾克斯“被CORS政策封锁”

时间:2020-02-21 07:25:28

标签: javascript ajax https cors

喜欢加载页面的https版本(如果可用)。我的代码“被CORS政策阻止”。似乎应该阻止安全连接是愚蠢的。我可以仅使用javascript来解决此问题,而无需添加其他服务器端代码吗? (并且不使用jquery)。

// check if ssl version of the page is available
if (window.location.protocol=="http:") { 
	newurl = "https:"+window.location.href.substr(5);

	// get an ajax object
	if (window.XMLHttpRequest) req=new XMLHttpRequest();
	else if (window.ActiveXObject) req=new ActiveXObject("Microsoft.XMLHTTP");
	else req=false;
	
	// run ajax
	if (req) {
		// setup return function to load page if it was successful
		req.onreadystatechange=function() {
			if (req.readyState!=4) return;
			if (req.status==200) location.href=newurl;
		}
		// attempt to open url
		req.open("GET",newurl,true);
		req.send("");
	}
}

0 个答案:

没有答案