java - POST request returns 415 error -
i'm trying call post request (using jersey api rest) html form using angularjs v 1.5.8.
have html form submit button calls rest serivce:
<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>
this logincontroller script:
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 $scope.data } ) } } )
and here simple rest post service:
@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(); } }
the post request called returns 415 error: the server refused request because request entity in format not supported requested resource requested method.
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 error occurs // or server returns response error status. }); }
}
Comments
Post a Comment