2016-06-28 4 views
0

AngularJS v 1.5.8을 사용하는 HTML 양식에서 POST 요청 (REST 용 Jersey APi 사용)을 호출하려고합니다.POST 요청이 415 오류를 반환합니다.

<body ng-app="myApp" ng-controller="loginController"> 
...... 
<form name="myForm" nonvalidate ng-submit="login()"> 

...... 

<div 
class="col-md-4 col-md-offset-4 col-sm-4 col-sm-offset-3 col-xs-6 col-xs-offset-3"> 
<button type="submit" class="btn btn-default" ng-submit="login()">Submit</button> 
</div> 
</form> 

이 내 인 LoginController 스크립트입니다 :

var app = angular.module ("myApp", []); 
app.controller("loginController", function($scope, $http){ 

    $scope.username = ""; 
    $scope.password = ""; 

    $scope.login = function() { 

     var transform = function(data) { 
      return $.param(data); 
     } 

     $http(
       { 

        url : 'http://localhost:8080/RestServices/services/user/add', 
        method : 'POST', 
        headers : { 
         'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' 
        }, 

        transformRequest : function(data) { 
         return $.param(data); 
        }, 
        data: { name:  $scope.username, 
          password: $scope.password } 

       }).success(function(response, status) { 
        $scope.data = response; //Assign data received to $scope.data 
       } 
       ) 
    } 
} 
) 

그리고 여기 내 간단한 REST 포스트 서비스 :

@Path("user") 
public class UserResource { 

    private TreeMap<Integer, User> userMap = new TreeMap<Integer, User>(); 


    @POST 
    @Path("add") 
    @Consumes({MediaType.APPLICATION_JSON}) 

    public Response addUser(User user) { 

     int id = userMap.size(); 
     user.setId(id); 
     userMap.put(id, user); 

     ObjectMapper mapper = new ObjectMapper(); 
     String jsonString = ""; 
     try { 
      jsonString = mapper.writeValueAsString(user); 
     } catch (JsonProcessingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      jsonString = null; 
     } 

     return Response.ok().entity(jsonString).build(); 

    } 

} 

나는 REST의 serivce를 호출하는 전송 버튼이있는 HTML 양식이

POST 요청이 호출되었지만 415 오류를 반환합니다. 요청 엔터티가 있기 때문에 서버가이 요청을 거부했습니다. 요청 된 메소드에 대해 요청 된 자원이 지원하지 않는 형식.

+3

난 당신이 각도 JS 헤더 부분에 문제가있다 생각합니다. content type = "application/x-www-form-urlencoded"라고 쓰고 JSON으로 데이터를 보내면 웹 서비스도 JSON을 기대합니다. angularJS의 콘텐츠 유형을 JSON – rahul

+0

으로 변경해야합니다. 콘텐츠 유형을 설정하려고했습니다 : '{ 'Content-Type': 'application/json; charset = utf-8'}'그러나 작동하지 않습니다. 동일한 415 오류가 있습니다. – DistribuzioneGaussiana

+0

요청에서 charset = utf-8을 삭제 해주십시오. 게시물에 대한 답변보기 http://stackoverflow.com/questions/22566433/http-415-unsupported-media-type-error-with-json – dasrohith

답변

0
function loginController($scope, $http) { 
$scope.username = ""; 
$scope.password = ""; 

$scope.login = function() { 

    $http({ 
     method: 'POST', 
     url: 'user/add', 
     data: { 
      username: $scope.username, 
      password: $scope.password 
     } 
    }).success(function (data, status, headers, config) { 

     $scope.result =data.username; 
     alert($scope.result); 
    }).error(function (data, status, headers, config) { 
     // called asynchronously if an error occurs 
     // or server returns response with an error status. 
    }); 
} 

}

관련 문제