2016-08-04 3 views
0

양식을 제출할 때 405 오류가 계속 발생합니다. 여기 내 컨트롤러 :Angular.js 양식 POST 오류 405가 허용되지 않습니다.

'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는 내보기 :

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

다른 파일이나 내가 이런 일 할 수 있도록 수정해야 할 일이 있습니까? 내가 뭘 놓치고 있니?

+0

귀하는 해당 URL이 올바른지 확인하십시오. 즉, 'url :'/ leads ''. 이건 api 호출인가? –

+0

/leads는 양식 데이터를 처리하기 위해 로컬 일 것입니다 – Dubster

답변

1

서버는 특정 방법 (GET, POSTPUT, PATCH, DELETE 등) 서버에 의해 허용 될 수 없음을 말하고있다. 이는 API 경로/종단점에 POST 메소드가 구성되어 있지 않기 때문일 가능성이 큽니다.

예를 들어, ExpressJS에서 다음을 수행하여 POST을 허용하는 경로를 구성해야 할 것 :

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

405 Method Not Allowed 오류, click here에 대한 자세한 내용은.

+0

나는 이런 식으로 생각했습니다. 나는 각도로 POST를 받아들이도록 라우트를 구성하는 방법을 확신하지 못합니다. – Dubster

+0

'$ http' 요청으로 이미 POST를 보내고 있습니다. 문제는 각도가 아니라 API에 있습니다. 귀하의 API는 어떤 언어 또는 프레임 워크로 작성 되었습니까? – Soviut

+0

아마도 실제 문제 일뿐입니다. 아직 양식 처리기를 설정하지 않았습니다. – Dubster

0

몇 주 전에 비슷한 문제가있었습니다. 서버가 헤더에 특정 유형의 'Content-Type' 만 수락하는 것으로 나타났습니다.

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

P.S. :

그러나 애플리케이션에 Content-Type 변경/JSON 문제를 해결 나는 이것이 당신의 문제를 반드시 해결할 것이라고 말하지는 않지만, 이것은 이러한 유형의 문제와 관련된 해결책입니다. 서버에서 어떤 종류의 입력을 기대하는지 알아보십시오.


편집

은 내가 Content-Type'application/json'를 보낼 말하고 있지 않다. 내가 말하는 건 허용되는 콘텐츠 유형을 찾고 해당 유형의 헤더를 보냅니다.

+0

그러면 '405 Method Not Allowed'와 다른 오류가 반환됩니다. 아마도 당신은'406 Not Acceptable'을 생각하고 있을까요? – Soviut

+0

''Content-Type'으로''application/json ''을 보내지 않을 것입니다.내가 말하고자하는 것은 ** 어떤 유형의 콘텐츠가 받아 들여지고 그런 유형의 헤더를 보내는지를 찾는 것입니다. ** –

+1

콘텐츠 유형이 무엇인지는 중요하지 않습니다. 콘텐츠 형식이 허용되지 않으면 405가 아니라 406 오류가 발생합니다. – Soviut

관련 문제