![](/img/trans.png)
[英]XMLHttpRequest() POST returns 405 error; method not allowed
[英]Angular.js Form POST Returns Error 405 Method Not Allowed
嘗試提交表單時,我一直收到405錯誤。 這是我的控制器:
“嚴格使用”;
angular.
module('myApp').
component('zipPopup', {
templateUrl: 'zip-popup/zip-popup.template.html',
controller: function ZipPopupController($scope, $http) {
$scope.show = true;
$scope.hide = function(){
$scope.show = false;
};
$scope.user = {};
$scope.regex = '\\d{5}';
$scope.submitForm = function(isValid) {
if(isValid) {
$http({
method : 'POST',
url : '/leads',
data : $scope.user,
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(response) {
$scope.show = false;
})
.error(function(response) {
console.log(response);
});
}
};
}
});
這是我的觀點:
<div class="zip-popup text-center">
<div class="popup-container">
<form name="zipForm" class="zip-form" ng-submit="submitForm(zipForm.$valid)" novalidate>
<input ng-model="user.zipCode" ng-pattern="regex" name="zipCode" class="form-control text-center" placeholder="Your Zip" required />
<p class="error" ng-show="zipForm.zipCode.$invalid && !zipForm.zipCode.$pristine">A 5 digit zip code is required.</p>
<button type="submit" class="btn btn-block" ng-disabled="zipForm.$invalid">Submit</button>
</form>
</div>
</div>
是否有其他文件或需要修改的文件才能發生? 我想念什么?
服務器告訴您特定的方法( GET
, POST
, PUT
, PATCH
, DELETE
等)無法被服務器接受。 這很可能是由於您的API路由/端點未配置POST
方法。
例如,在ExpressJS中,您需要通過執行以下操作來配置接受POST
的路由:
app.post('/leads', function (req, res) {
res.send('POST request to the homepage');
});
有關405 Method Not Allowed
錯誤的更多信息, 請單擊此處 。
幾周前我也遇到過類似的問題。 事實證明,服務器僅在標頭中接受特定類型的'Content-Type'
。
但是將Content-Type
更改為application / json解決了該問題:
$http({
method : 'POST',
url : '/leads',
data : $scope.user,
headers : {'Content-Type': 'application/json'}
})
.success(function(response) {
$scope.show = false;
})
.error(function(response) {
console.log(response);
});
PS我並不是說這一定能解決您的問題,但這是與此類問題有關的解決方案。 只需找出您的服務器期望什么樣的輸入即可。
編輯
我並不是說要發送'application/json'
作為Content-Type
。 我要說的是, 查找可接受的內容類型並發送該類型的標題。
您嘗試讓后端工程師實現OPTIONS方法。
https://en.wikipedia.org/wiki/Cross-origin_resource_sharing#Preflight_example
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.