需要使用JavaScript(www.domain1.com)中的基本身份驗證進行REST GET調用。 REST API位於domain2.com中。 REST API返回CORS特定的HTTP頭。我需要支持IE,Chrome和Firefox。下面的JavaScript解釋了問題的進一步 -基於XMLHttpRequest的CORS基本身份驗證調用在Firefox和Chrome中失敗
沒有基本認證(在IE 10,Chrome和火狐,是由XDomainRequest對象照顧年長的IE)
var xhr = new XMLHttpRequest(); xhr.open(method, url, true);
使用基本身份驗證(作品IE 10只,未能在鉻和Firefox)
var xhr = new XMLHttpRequest(); xhr.open(method, url, true, username, password);
隨着基本認證(未能在鉻和Firefox)
var xhr = new XMLHttpRequest(); xhr.open(method, url, true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr. withCredentials = 「true」;
使用基本身份驗證(在Chrome和Firefox失敗)
$.ajax({ type: 'GET', url: jsonp_url, dataType: "json", username: username, password: pwd, beforeSend: function (xhr) {xhr.withCredentials = true; }, success: function (result) { }, error: function (xhr, ajaxOptions, thrownError) { }
我喜歡這裏,在Chrome和Firefox與基本驗證
你可以發佈迴應標題嗎? – idbehold