0

I'm trying to post data from Angular to my servlet. But, it throws me the error

"Failed to load resource: the server responded with a status of 405 (Method Not Allowed)"

Here is my code. Am I missing anything?

$scope.pushDataToServer = function() {      
    $scope.data = {user_id:"123",key_name:"key2",value:"value2"};
    $http({
        method: 'POST',
        url: 'pushData',
        headers: {'Content-Type': 'application/json'},
        data: $scope.data
        }).success(function (data){
            $scope.status=data;
        }).error(function(data, status, headers, config) {
            alert("error")
       });

};

My servlet config

    <servlet>
     <servlet-name>pushData</servlet-name>
     <servlet-class>com.data.pushData</servlet-class>
 </servlet>

 <servlet-mapping>
     <servlet-name>pushData</servlet-name>
     <url-pattern>/pushData</url-pattern>
 </servlet-mapping>
JSmith
  • 4,519
  • 4
  • 29
  • 45
Syed
  • 2,471
  • 10
  • 49
  • 89
  • 405 errors can arise because the Web server is not configured to take data from the client at all or client does not have sufficient authority – moh Feb 10 '16 at 05:49

2 Answers2

0

write a do post method in your servlet.

 /**
 * handles HTTP POST request
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    //TODO: handle POST here
 }
manoj
  • 3,391
  • 2
  • 20
  • 30
  • This is my request object $scope.data = {"user_id":"123","key_name":"key2","value":"value2"};.. But still why its getting null value in my servlet? I'm using request.getParameter("key_name") – Syed Feb 10 '16 at 04:56
  • try the above link, you are sending data as a json, the above links explains it, once you get the json body `jb` you can use any JSON implementation to parse it. – manoj Feb 10 '16 at 04:58
  • refer this too.. http://stackoverflow.com/questions/1548782/retrieving-json-object-literal-from-httpservletrequest – manoj Feb 10 '16 at 05:03
-1

Simple, the servlet you are calling doesn't support the POST method.

You haven't implemented it or has done so erroneously.

Thihara
  • 7,031
  • 2
  • 29
  • 56
  • Ah yes! I've put the code in doGet.. It should be in doPost.. Thx Thihara – Syed Feb 10 '16 at 04:52
  • This is my request object $scope.data = {"user_id":"123","key_name":"key2","value":"value2"};.. But still why its getting null value in my servlet? I'm using request.getParameter("key_name") – Syed Feb 10 '16 at 04:55