无法使用XMLHttpRequest()将数据从http站点检索到https

Unable to retrieve data from http site to https using XMLHttpRequest()

本文关键字:http 站点 点检索 https 数据 XMLHttpRequest      更新时间:2023-09-26

我有一个托管在https://上的站点,我想在其中从显示共享属性的站点中提取数据。返回数据的URL为:

http://ir1.euroinvestor.com/asp/ir/xmlirmultiiso2.aspx?companyid=281191

我为获得数据而开发的代码如下:

function GetSharesUpdate(){
    // makeing AJAX calls to the web service for getting the share update    
    var xhr = new XMLHttpRequest(); // Set up xhr request
    xhr.open("GET", "http://ir1.euroinvestor.com/asp/ir/xmlirmultiiso2.aspx?companyid=281191", true);   // Open the request
    xhr.responseType = "";   // Set the type of response expected    
    xhr.send();
    //  Asynchronously wait for the data to return
    xhr.onreadystatechange = function () {
        if (xhr.readyState == xhr.DONE) {
            var tempoutput = xhr.responseXML;
            alert(tempoutput);
        }
    }
    //  Report errors if they happen
    xhr.addEventListener("error", function (e) {
        console.error("Error: " + e + " Could not load url.");
    }, false);       
}

我在语句xhr.send();上得到错误,如下所示:

混合内容:位于"https://[SiteUrl]"的页面是通过https加载的,但请求了一个不安全的XMLHttpRequest端点'http://ir1.euroinvestor.com/asp/ir/xmlirmultiiso2.aspx?companyid=281191"。此请求已被阻止;必须通过HTTPS提供内容。

如果我将URL更改为https,即

https://ir1.euroinvestor.com/asp/ir/xmlirmultiiso2.aspx?companyid=281191

则在没有任何错误的情况下执行CCD_ 4,但是我得到CCD_。

我应该对代码进行哪些更改才能使其正常工作?

第一个问题是您正在对非https域进行ajax请求,但您的域已安装SSL。

第二个问题是跨源请求CORS,即使你首先解决了这个问题(即,即使你尝试了来自非http域的ajax请求(,你也会遇到这个问题。

由于您正在从另一个网站请求数据,除非请求的服务器配置为为您的域提供请求,否则这种情况最有可能发生要解决此问题,您需要从服务器(代理服务器(调用数据,或者将请求的服务器配置为允许来自域的请求。

查看更多-https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS