2017-04-13 52 views
0

我有一個在Azure上託管的ASP.NET Web API。我已將HTTP消息處理程序集成到該API。當我使用AJAX調用它時,消息處理程序正在被擊中。我也在我的AJAX調用中發送一些請求頭。問題是我沒有收到從AJAX調用發送到集成在Web API中的Message Handler的頭文件。請參閱圖像:Debugger screenshot無法從ASP.NET Web API中的jQuery AJAX請求中獲取標頭

下面是我的AJAX請求的代碼:

$.ajax({ 
    url: "https://someservicename.azurewebsites.net/Service/RequestHandler", 
    beforeSend: function (xhr) { 
     xhr.setRequestHeader('Token', '86810e135958961ad4880ad'); 
    }, 
    type: "POST", 
    crossDomain: true, 
    data: { 
     param: "Passed Parameter" 
    }, 
    success: function (msg) { 
     console.log(msg); 
    }, 
    error: function (XMLHttpRequest, textStatus, errorThrown) { 
     console.log(textStatus); 
    } 
}); 

中的Web API的消息處理程序看起來象下面這樣:

protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 
{ 
    string authToken; 
    try 
    { 
     authToken = request.Headers.GetValues("Token").FirstOrDefault(); 

     if (authToken != "86810e135958961ad4880ad") 
     { 

      return await Task.Factory.StartNew(() => 
      { 
       return new HttpResponseMessage(HttpStatusCode.BadRequest) 
       { 
        Content = new StringContent("Unauthorized access!") 
       }; 
      }); 
     } 
     else 
     { 
      return await base.SendAsync(request, cancellationToken); 
     } 
    } 
    catch (System.InvalidOperationException) 
    { 
     return null; 
    } 
} 

當我從IIS中運行的代碼本地主機發送的請求如下所示:

Accept:*/* 
Accept-Encoding:gzip, deflate, sdch, br 
Accept-Language:en-US,en;q=0.8 
Access-Control-Request-Headers:token 
Access-Control-Request-Method:POST 
Connection:keep-alive 
Host:someservicename.azurewebsites.net 
Origin:http://localhost:12522 
Referer:http://localhost:12522/pocppd.html 
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 

而負責人我在控制檯上得到的是:

https://someservicename.azurewebsites.net/Service/RequestHandler. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:12522' is therefore not allowed access. The response had HTTP status code 500. 

與POSTMAN客戶端請求工作正常,但從本地主機它會返回一個錯誤。請讓我知道沒有在我的消息處理程序中獲取標題的原因?

+0

響應哪位會帶來幾十[結果](https://www.google.com.tr/search?q=Response+to+preflight+request+doesn %27噸+傳+訪問+對照+檢查%3A + NO +%27Access - 控制 - 允許來源%27&OQ =響應+至+預檢+請求+沒有按%27噸+傳+訪問+對照+檢查%3A + NO +%27Access -Control-Allow-Origin%27&aqs = chrome..69i57j69i59.119j0j7&sourceid = chrome&ie = UTF-8)在搜索時只是告訴你,服務器上不允許使用哪個請求的來源,因爲CORS標準。 Marius的回答是一種使服務器響應來自你本地主機的請求的方法。 – ibubi

回答

1

你必須在你的web.config添加這個(因爲你需要,你可以更改值):

<httpProtocol> 
     <customHeaders> 
      <add name="Access-Control-Allow-Origin" value="*" /> 
      <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Authorization, Token" /> 
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" /> 
     </customHeaders> 
    </httpProtocol> 

請求前,一個OPTIONS請求被髮送,以確保您被允許執行請求。正如錯誤所述,「對預檢請求的響應不會通過訪問控制檢查:否請求的資源上存在'Access-Control-Allow-Origin'標頭。」在Web.config中添加上面的行將傳遞所需的標題。

MDN表示:「與上面討論的簡單請求不同,」preflighted「請求首先向另一個域上的資源發送HTTP OPTIONS請求頭,以確定實際請求是否安全發送。現場請求是這樣預先顯示的,因爲它們可能會影響用戶數據。「

+0

我在web.config中添加了上面的內容,但仍面臨同樣的問題......它突破了預檢請求...... AJAX以「Access-Control-Request-Headers:授權」的形式發送頭信息_without_其值 –

+0

Thanks @Marius ,我發現我的請求是預衝的,它沒有攜帶自定義頭文件,當發送預檢後的實際請求被髮送時,自定義頭文件與它一起發送! –

+0

我很高興聽到您設法修復它。上面的 –

1

嘗試在控制檯此

$.ajax({ 
    url: "https://someservicename.azurewebsites.net/Service/RequestHandler", 
    headers: { 
      "Token": "86810e135958961ad4880ad" 
     },  
    type: "POST", 
    crossDomain: true, 
    data: { 
     param: "Passed Parameter" 
    }, 
    success: function (msg) { 
     console.log(msg); 
    }, 
    error: function (XMLHttpRequest, textStatus, errorThrown) { 
     console.log(textStatus); 
    } 
}); 
+0

不起作用...同樣的問題 –