問題背景:的WebAPI CORS - 訪問控制允許來源頭出現在請求錯誤
這似乎是一個常見的問題,我有下降的犯規。
我目前在Azure中託管一個標準的WebAPI和調用AngularJS應用程序,在上述的WebAPI調用一個終點。
的AngularJS應用程序URL調用到的WebAPI是:
http://siteang.azurewebsites.net
而且的WebAPI地址是:
https://site.azurewebsites.net
我想,以確保只有我在http://siteang.azurewebsites.net
的應用程序能夠訪問的WebApi at https://site.azurewebsites.net
問題:
我收到了我的AngularJS應用到的WebAPI服務的提交形式出現以下錯誤。
XMLHttpRequest cannot load https://site.azurewebsites.net/api/ShoppingComparison/GetC…itemIndex=Baby&itemtosearch=baby&lowToHigh=false&maxPrice=50000&minPrice=0.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://siteang.azurewebsites.net' is therefore not allowed access. The response had HTTP status code 500.
驗證碼:
以下是$ HTTP請求到的WebAPI服務。
注意,通話的頭屬性已設置與AngularJS站點的地址。
loadSearchList: function (itemToSearch, itemIndex, countryCode) {
self.itemToSearch = itemToSearch;
self.itemCatagory = itemIndex;
self.country = countryCode;
self.searchList = [];
$http({
method: 'GET',
url: 'https://site.azurewebsites.net/api/ShoppingComparison/GetComparisons',
params: {
itemtosearch: itemToSearch,
itemIndex: itemIndex,
countryCode: countryCode,
maxPrice: '50000',
minPrice: '0',
highToLow: true,
lowToHigh: false,
amazonEbay: true,
amazonOnly: false,
ebayOnly: false
},
headers: {
'Content-Type': 'text/plain; charset=UTF-8',
'Access-Control-Allow-Origin': 'http://siteang.azurewebsites.net',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT, DELETE'
}
}).success(function (data) {
//Success Handler.
}).error(function (data) {
//Error Handler.
});
}
以下是由AngularJS應用程序調用的WebApi控制器。請注意,頭已被設置爲接受http://siteang.azurewebsites.net
站點進行調用:
[System.Web.Http.HttpGet]
[EnableCors(origins: "http://siteang.azurewebsites.net", headers: "*", methods: "*")]
public ViewItemModel GetComparisons([FromUri] ComparisonRequestModel comparisonModel)
{
return _callAndSearchApis.SearchApis(comparisonModel);
}
任何幫助determing爲什麼這個請求是由控制器的WebAPI拒絕將非常感激。
它有認證嗎? – Dreamweaver
你測試了哪些瀏覽器?有一些瀏覽器不支持CORS:http://caniuse.com/cors –
你調用了'config.EnableCors();'? –