简体   繁体   English

Angular.js表单POST返回错误405方法不允许

[英]Angular.js Form POST Returns Error 405 Method Not Allowed

I keep getting a 405 error when trying to submit my form. 尝试提交表单时,我一直收到405错误。 Here's my controller: 这是我的控制器:

'use strict'; “严格使用”;

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);
                    });
                }
            };
        }
    });

Heres my view: 这是我的观点:

<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>

Is there a different file or something that I need to modify to allow this to happen? 是否有其他文件或需要修改的文件才能发生? What am I missing? 我想念什么?

The server is telling you that the particular method ( GET , POST , PUT , PATCH , DELETE , etc.) can't be accepted by the server. 服务器告诉您特定的方法( GETPOSTPUTPATCHDELETE等)无法被服务器接受。 This is most likely due to the fact that your API route/endpoint doesn't have a POST method configured. 这很可能是由于您的API路由/端点未配置POST方法。

For example, in ExpressJS you'd need to configure a route that accepts POST by doing the following: 例如,在ExpressJS中,您需要通过执行以下操作来配置接受POST的路由:

app.post('/leads', function (req, res) {
  res.send('POST request to the homepage');
});

For more information on the 405 Method Not Allowed error, click here . 有关405 Method Not Allowed错误的更多信息, 请单击此处

I had a similar issue a couple of weeks ago. 几周前我也遇到过类似的问题。 It turns out that the server was only accepting 'Content-Type' of a particular type in the headers. 事实证明,服务器仅在标头中接受特定类型的'Content-Type'

However changing the Content-Type to application/json solved the issue: 但是将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 I'm not saying this will necessarily solve your issue, but this is a solution related to this type of problem. PS我并不是说这一定能解决您的问题,但这是与此类问题有关的解决方案。 Just find out what kind of input your server is expecting. 只需找出您的服务器期望什么样的输入即可。


EDIT 编辑

I'm not saying to send 'application/json' as the Content-Type . 我并不是说要发送'application/json'作为Content-Type All I'm saying is that Find what type of content is acceptable and send that type of header. 我要说的是, 查找可接受的内容类型并发送该类型的标题。

you try to let the back-end engineer to implement the OPTIONS method. 您尝试让后端工程师实现OPTIONS方法。

https://en.wikipedia.org/wiki/Cross-origin_resource_sharing#Preflight_example https://en.wikipedia.org/wiki/Cross-origin_resource_sharing#Preflight_example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM