2012-11-25 87 views
7

我在我的網站上做了一些非常基本的jQuery ajax的東西,我遇到了一大堆麻煩。jQuery ajax不會發出HTTPS請求

下面是相關代碼:

$(document).ready(function() { 
    $("#getdatabutton").click(function() { 
     $.ajax({ 
      url: "/jsontest/randomdata", 
      type: "get", 
      data: [{name:"ymax", value:$("#randomgraph").height()}, 
        {name:"count", value:$("#countinput").val()}, 
        {name:"t", value:Math.random()}],  
      success: function(response, textStatus, jqXHR) { 
       data = JSON.parse(response); 
       updateGraph(data); 
       $("#result").html(response); 

       if(data["error"] == "") { 
        $("#errorbox").html("None"); 
       } 
       else { 
        $("#errorbox").html(data["error"]); 
       } 
      }, 
      error: function(jqXHR, textStatus, errorThrown) { 
       $("#errorbox").html(textStatus + " " + errorThrown); 
      } 
     }); 
    }); 
}); 

加載頁面時通過HTTPS,但似乎XMLHttpRequest的通過HTTP出去。

我試圖將網址更改爲絕對網址(https://larsendt.com/jsontest/randomdata),並且它仍然將請求發送到我的網站的HTTP版本。

當然,由於請求將轉到不同的協議,所以ajax調用失敗(跨域和全部)。

據報道由鉻:

The page at https://larsendt.com/jsontest/ displayed insecure content from http://larsendt.com/jsontest/randomdata/?ymax=500&count=32&t=0.08111811126582325. 

唯一的其他有關我能想到的信息是,我有nginx的做301重定向從http://larsendt.comhttps://larsendt.com,但我看不出那會打破任何東西(我相信這是相當標準的做法)。

如果你想要一個現場演示,破損版本仍然在https://larsendt.com/jsontest

無論如何,在此先感謝。

+0

你介意改變你的帖子的標題,讓這個查詢的人不會登陸這個頁面。 – brayne

回答

13

嘗試固定的網址,以便您的服務器沒有重定向

url: "/jsontest/randomdata/" // there was a missing trailing/

// i.e. https://larsendt.com/jsontest/randomdata?ymax=500&count=32&t=0.9604179110508643 
// was going to https://larsendt.com/jsontest/randomdata/?ymax=500&count=32&t=0.9604179110508643 
+0

啊!那樣做了。非常感謝! –

+0

好點,Nood。這可能也可以在服務器中修復。 IIRC,在Apache(也許還有其他)中,有幾種不同的方法來處理丟失的斜線。我不是專家,但曾經閱讀過一次(大約10年前!)。 –

+0

這解決了我的問題。非常好的一點。 –

1

301永久重定向可能會發生。檢查運行Fiddler並觀察結果欄。通常200碼,但我發現了301碼。

HTTPS jQuery的AJAX調用被重定向到HTTP,造成混合內容錯誤。