2013-03-11 120 views
2

jQuery 1.9.1jQuery ajax授權令牌標頭失敗

請求永遠不會被髮送,似乎只是出錯。 console.log輸出顯示Request failed: error我不知道如何進一步調試。

$.ajax({ 
    beforeSend: function (xhr) { xhr.setRequestHeader ('Authorization', 'Token c576f0136149a2e2d9127b3901015545') }, 
    type: "GET", 
    url: "https://example.com/endpoint", 
    dataType: "json" 
}).done(function(msg) { 
    console.log(msg); 
}).fail(function(jqXHR, textStatus, errorThrown) { 
    console.log("Request failed: " + textStatus); 
    console.log(errorThrown); 
}); 

檢查谷歌瀏覽器的網絡流量顯示

  • Method: OPTIONS
  • Status: (canceled)
  • Type: Pending

但是使用curl正常工作:

curl https://example.com/endpoint -H 'Authorization: Token token="c576f0136149a2e2d9127b3901015545"'

這裏是的jsfiddle:http://jsfiddle.net/2vtyD/1/

注:這是不是Access-Control-Allow-Origin一個問題,因爲我有這部分工作。

我知道我必須失去了一些東西......

更新:

一切正常,當我切換到http。只有在使用https時纔會出現此問題。我認爲這可能是因爲我正在使用自簽名證書(無效)。但其他SSL請求(非授權,即不是'OPTIONS')工作得很好。所以它似乎只是SSL和OPTIONS請求的組合。 chrome中的狀態表示取消,從查看服務器日誌apache日誌顯示一些活動,但它永遠不會成爲rails web應用程序。

下面是Apache日誌的sniplet:

[info] [client 10.0.2.2] (70007)The timeout specified has expired: SSL input filter read failed. 
[info] [client 10.0.2.2] Connection closed to child 10 with standard shutdown (server example.com:443) 
[info] [client 10.0.2.2] Connection to child 11 established (server example.com:443) 
[info] Seeding PRNG with 656 bytes of entropy 
[info] [client 10.0.2.2] (70014)End of file found: SSL input filter read failed. 
[info] [client 10.0.2.2] Connection closed to child 11 with standard shutdown (server example.com:443) 

然而,這些類型的消息正在發生的所有SSL請求。所以我基本上對如何繼續下去感到茫然。

回答

1

使用有效的SSL證書。

由於某些原因,GET,POST,PUT和DELETE請求可以正常使用自簽名證書。但是OPTIONS請求沒有。使用有效的SSL證書,一切都會正常工作。