Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending request with JSON returns error 405 :Method not allowed

I am trying to send intakeanswers.json from AngularJS form by clicking on the Edit button to java web service but I am getting an ERROR:

"405 Method not allowed".

Is my JSON fine?

AngularJS and HTML:

<!DOCTYPE html>
<html ng-app="myappp">
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
        <script src="lib/js/angular.min.js"></script>
        <script src="lib/js/angular-route.min.js"></script>
        <script src="lib/js/angular-ui-router.min.js"></script>
    </head>
    <body ng-controller="edit_ctrl">

        <script type="text/javascript">
            var myapp = angular.module('myappp', []);
            myapp.controller('edit_ctrl', function($scope, $http) {
                $http.get("data/intakeanswers.json").success(function(data) {
                    $scope.dataJson=data;
                });
                $scope.doEdit = function() {
                    $scope.dataJs = angular.copy($scope.dataJson);
                    var dat = angular.toJson($scope.dataJs);
                    alert("hi");
                    $http({
                         url: "http://localhost:8080/...../FormAnswers/Answers",
                         method: 'POST',
                         data: dat,
                         headers: {'Content-Type': 'application/Json'},
                    })
                };          
            });
        </script>
        <button ng-click="doEdit()">Edit</button>
    </body>
</html>

Java rest web service code:

@Path("/FormAnswers")
public class FormAnswersService {

    private static final Logger logger = Logger.getLogger(FormAnswersService.class);

    @GET
    @Path("/Answers")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getAnswers(String id) {
        try {       
            System.out.println(id);     
        }
        catch(Exception e) {
            logger.error(e.getMessage());           
        }
        return "response";  
    }
}

JSON:

{ 
    "User_Id": "abcd",
    "Form_Name": "Form",
    "Answers": [{
            "Question_Id": "1",
            "Answer" :"a"
        },
        {
            "Question_Id": "2",
            "Answer" :"b"
        },
        {
            "Question_Id": "3",
            "Answer" :"c"
        }
    ],
    "Section_Name": "Project Overview",
    "Answers": [{
            "Question_Id": "4",
            "Answer" :"d"
        },
        {
            "Question_Id": "5",
            "Answer" :"e"
        },
        {
            "Question_Id": "6",
            "Answer" :"f"
        }
    ]       
}
like image 297
user3449214 Avatar asked Aug 17 '26 02:08

user3449214


1 Answers

Your web service controller only declared a GET method implementation. You need to add the support for POST:

@POST
@Path("/Answers")
@Consumes(MediaType.APPLICATION_JSON)
public Response setAnswers(String data) {

}

Since, your web-service doesn't support POST, 405 is expected.

like image 89
Raunak Agarwal Avatar answered Aug 18 '26 18:08

Raunak Agarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!