2012-05-24 42 views
0

我嘗試使用ajax(xmlHttpRequest)來使用WCF服務(REST)。該服務需要基本身份驗證。Ajax回調無法找到授權標頭

我的Ajax調用:

var httpRequest = new XMLHttpRequest(); 
     httpRequest.onreadystatechange = function() { 
      if (httpRequest.readyState == 4) { 
       if (httpRequest.status == 200) { 
        //do some stuff 
       } 
      } 

     }; 
     httpRequest.open('PUT', 'http://localhost:59000/v1/users/1', true, 'user1', 'user1'); 
     httpRequest.withCredentials = "true"; 
     //must authenticate both..in open() but also set header manually ...cf http://stackoverflow.com/questions/1358550/xmlhttp-request-basic-authentication-issue 
     httpRequest.setRequestHeader('Auhtorization', 'Basic user1:user1'); 
     httpRequest.setRequestHeader('Accept', 'application/json'); 
     // overridemimeType() does not set content type header .... don't know why ? 
     httpRequest.setRequestHeader('Content-Type', 'application/json'); 
     var params = { "UserName": "user1" }; 
     var requestBodyString = JSON.stringify(params); 
     httpRequest.send(requestBodyString); 

我第一次處理在服務器端請求的方式如下

​​

我的瀏覽器發送我的錯誤「請求頭字段Auhtorization不由Access-Control-Allow-Headers控制「,但正如你所看到的,它在響應頭部變成白色。

此外,當我嘗試與提琴手一切都很好,我甚至允許標題虛擬一個。

所以我真的很困惑,如果有人可以幫忙,請做!

感謝

+0

您正在使用哪種瀏覽器? – VJAI

+0

谷歌瀏覽器! – laurent

回答

0

得到它,而運行你的「使用Visual Studio開發服務器」異常被拋出當您嘗試添加頁眉(第二代碼塊)服務器:「此操作需要IIS綜合管道模式」。

的解決方法是在應用程序配置爲「使用IIS Web服務器」

因此這個環節http://msdn.microsoft.com/en-us/library/d14azbfh.aspx#addexceptionscommand

你不能要求Visual Studio來告訴你什麼時候拋出異常,我錯過了。

感謝

0

也許這是不相關的,但阿賈克斯片段上方還拼錯的標題名稱爲「Auhtorization」而不是「授權」。

+0

你是對的,但它並沒有解決問題。不管怎麼說,還是要謝謝你 – laurent