我試圖做一個POST到api,但我得到一個'NetworkError 405 - 方法不允許'什麼我來學習的是一個預先選擇的OPTIONS呼叫服務。研究這個問題的答案不是我想要的。我曾與CORS的首要問題,而我添加了以下到我的web服務(網頁API):
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
我有我的客戶端HTML /角碼安排如下:
HTML:
<div class="row">
<div class="col-lg-6 col-xs-12">
<div class="form-group">
<label>First Name*</label>
<input type="text" id="txtFirstName" ng-model="firstName" class="form-control" required/>
</div>
</div>
<div class="col-lg-6 col-xs-12">
<div class="form-group">
<label>Last Name*</label>
<input type="text" id="txtLastName" ng-model="lastName" class="form-control" required/>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6 col-xs-12">
<div class="form-group">
<label>Company Name*</label>
<input type="text" id="txtCompanyName" ng-model="companyName" class="form-control"
required/>
</div>
</div>
<div class="col-lg-6 col-xs-12">
<div class="form-group">
<label>Your Work Email*</label>
<input type="email" id="txtEmail" ng-model="email" class="form-control" required/>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-xs-12">
<div class="form-group">
<label>Office Phone Number*</label>
<input type="text" id="txtPhone" ng-model="phone" class="form-control" required/>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-xs-12">
<div class="form-group">
<input type="submit" id="btnDemoSubmit" class="btn btn-default form-control" style="background-color: #053A54; color: #ffffff;" value="Submit For Trial" ng-click="demoInquiry()"/>
</div>
</div>
</div>
控制:
(function() {
var app = angular.module("app", [])
app.controller('MainController', function($scope, $http) {
var onUpdatesComplete = function (response) {
$scope.updates = response.data;
};
$http.get("http://localhost:XXXXX/XXX/XXXXXX/XXXXXXXXXX")
.then(onUpdatesComplete);
$scope.demoInquiry = function(){
var data = {
firstName : $scope.firstName,
lastName : $scope.lastName,
companyName : $scope.companyName,
email : $scope.email,
phone : $scope.phone
};
console.log(data);
$http.post("http://localhost:XXXXX/XXX/XXXXXX/XXXXXXXXXX", data).success(function() {
$scope.postSuccess = true
}).error(function(){
$scope.postError = true;
});
};
});
}());
我已驗證服務與工作正常小提琴手,所以我不知道如何解決這個問題。任何幫助,將不勝感激。
這個回答對你有幫助嗎? http://stackoverflow.com/questions/17498691/enabling-cors-with-webapi-put-post-requests/17506774#17506774 – Badri 2014-10-01 02:05:14