我是angularjs的新手,嘗試使用angularjs客戶端來使用wcf寧靜服務。初期我嘗試了http.get(url)並獲得了CORS問題我通過在我想調用的wcf服務方法中添加以下代碼來解決問題。使用Angularjs和wcf寧靜服務製作http.post時發生CORS問題
WebOperationContext.Current.OutgoingResponse.Headers.Add( 「訪問控制允許來源」, 「」); WebOperationContext.Current.OutgoingResponse.Headers.Add(「Access-Control-Allow-Methods」,「POST,GET,OPTIONS」); WebOperationContext.Current.OutgoingResponse.Headers.Add(「訪問控制允許報頭」,「內容類型,接受」);當我發了帖子調用(http.post)同*
現在我的方式得到我得到的迴應與CORS問題。
但是,當我試圖通過JSON對象,同時發佈帖子時,我再次開始獲取CORS問題。
我的后角碼爲:
var requestData = {
RequestUserName: "Abc1",
RequestPass: "123"
};
var req = {
method: 'POST',
url: 'url',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
data: requestData
};
$http(req).success(function(){console.log("Success");
$scope.userDetails = response.UserNameAuthenticationResult;}).error(function(){console.log("Error");});
我的WCF經營合同看起來如下:
[WebInvoke(Method="POST",UriTemplate="/Authenticate"
,RequestFormat=WebMessageFormat.Json
,ResponseFormat=WebMessageFormat.Json
,BodyStyle = WebMessageBodyStyle.Wrapped)]
string UserNameAuthentication(Request request);
和方法的實現如下:
public string UserNameAuthentication(Request request)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");
return "true";
}
而且錯誤我得到如下:
**選項URL(匿名函數)@ angular.js:11442sendReq @ angular.js:11235serverRequest @ angular.js:10945processQueue @ angular.js:15552(匿名函數)@ angular.js:15568 $ @ EVAL angular.js:16820 $ digest @ angular.js:16636 $ apply @ angular.js:16928(匿名函數)@ angular.js:24551defaultHandlerWrapper @ angular.js:3398eventHandler @ angular.js:3386 localhost /:1 XMLHttpRequest無法加載 。對 預檢請求的響應未通過訪問控制檢查:否 「所請求的 資源上存在」Access-Control-Allow-Origin「標頭。原因'http://localhost:14703'因此不允許 訪問。該響應具有HTTP狀態碼405。
我認爲沒有什麼做angularjs。您需要在wcf應用程序中配置CORS。 [檢查出來](http://www.codeproject.com/Articles/845474/Enabling-CORS-in-WCF)。 –
使用此插件:https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl = en – Arjun
這沒有幫助,我已經嘗試把這段代碼放在調用方法內部在wcf服務,但仍然是相同的錯誤。此外,因爲我已經構建了wcf服務作爲類庫,所以我沒有選擇添加global.asax文件,但我們確實在wcf應用程序中獲得了它,因此我在調用方法中添加了Headers代碼。 – Nishant