2016-08-04 102 views
0

我有一個站點託管在https://上,我想從該站點獲取顯示共享屬性的網站數據。返回數據的網址是:無法使用XMLHttpRequest()從http站點檢索數據https()

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, 但要求一個insecu re XMLHttpRequest端點 'http://ir1.euroinvestor.com/asp/ir/xmlirmultiiso2.aspx?companyid=281191'。 此請求已被阻止;內容必須通過HTTPS提供。

如果我更改URL,以https

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

然後xhr.send();是沒有任何錯誤執行,但我得到xhr.responseXML空。

我應該怎樣改變我的代碼才能使它工作?

+0

你的第二個url中有四個斜線('/')。那是對的嗎? – FrankerZ

+0

感謝您的關注。添加帖子時出現輸入錯誤。 –

+0

它執行沒有任何錯誤?這不是當我運行它:* VM155 nizaqapusu.js:6 GET https://ir1.euroinvestor.com/asp/ir/xmlirmultiiso2.aspx?companyid=281191net :: ERR_INSECURE_RESPONSE *,當我訪問的網址鉻抱怨SSL證書用於不同的主機名。 – Quentin

回答

0

第一個問題是您正在對非https域執行ajax請求,但您的域已安裝SSL。

第二個問題是跨域請求CORS,即使您先解決(即使您嘗試來自非http域的ajax請求),您也會遇到此問題。

由於您正在從另一個網站請求數據,因此最有可能發生這種情況,除非所請求的服務器配置爲爲您提供請求域 要解決此問題,您需要從服務器(代理服務器)調用數據或者請求的服務器配置爲允許來自您的域的請求。

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