1
votes

I have been trying to get HTTP POST to work from AngularJS for several days. I’m really stuck right now, and I’d really appreciate any insight! For the server-side I am using JAX-RS and Jersey. The object I am passing is of type application/json, and I have created a Java class Student (please see code below for more details). My goal is to post a Student object from AngularJS to the server.

To verify that my logic was sound from the server-side I used a REST API client from Google Apps along with the Unix Command curl. Both methods returned HTTP status 200.

Yet when I tried the same thing from AngularJS, the HTTP POST has an error, with the Response object having no data and a status of -1. The post request seems to have reached the server, for it outputs HTTP status 204. What I do not understand is why with AngularJS I get a status code of 204 but with curl/REST API client I get a status of 200 and it works properly.

Below are the relevant parts of my code:

Controller definition and a function addAName() that contains a HTTP POST call:

app.controller('PostingController', function($scope, $http) {
$scope.postMessage='This message is a default message and will change after I click the button and addAName() is called.';
$scope.name='CODER';
$scope.postData = 'Not Available';

$scope.addAName = function(){
    var data = {"studentId": 2, "firstName": "Jason", "lastName": "Lee", "specialization": "Student"};

    $http.post("http:/localhost:8080/MathAssignment/studentservice/users", data).then( function successCallback(response) {
        $scope.postMessage = "POSTING WORKS!";
        $scope.postStatus = response.status;
        $scope.postData = response.data;
     }, function errorCallback( response ) {
        $scope.postMessage = "POSTING STILL DOES NOT WORK";
        $scope.postStatus = response.status;
        $scope.postData = response.data;
    });
};
});   

Server-Side code that accepts the HTTP POST call:

   @POST
   @Path("/users")
   @Produces(MediaType.APPLICATION_JSON)
   @Consumes(MediaType.APPLICATION_JSON)
   public Response updateUser(Student inputJsonObj) throws IOException {
      int id = (int) inputJsonObj.getStudentId();
      String name = (String) inputJsonObj.getFirstName() ;
      String profession = (String) inputJsonObj.getSpecialization();
      System.err.println("JSON is" + inputJsonObj.toString());
      System.err.println("Name is" + name);
      return Response.status(200).entity(inputJsonObj).build();
   }

Student class definition:

public class Student {

    private int studentId;
    private String firstName;
    private String lastName;
    private String specialization;

    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getSpecialization() {
        return specialization;
    }
    public void setSpecialization(String specialization) {
        this.specialization = specialization;
    }
}
1
You could also try looking at the raw packets -- one that works and one that doesn't -- and see if you can manually spot the difference. Network sniffer or TCPDump will help here. - markspace
Thanks for the fast reply. I did not create a WSDL file as this is a REST call. I thought they were only needed for SOAP/XML style services. Is this incorrect? - Joshua Trezle
No they are not. My client is running on localhost:63342, as I'm using the IDE Webstorm. My server is running on localhost:8080. - Joshua Trezle

1 Answers

1
votes

The REST service method's declaration includes:

@Consumes(MediaType.APPLICATION_JSON)

This indicates that the method consumes a content-type of "application/json".

In order to reach this method, you need to add a Content-type header with the value "application/json". Something like:

   $http.post("/foo/bar", requestData, {
        headers: { 'Content-type': 'application/json'}
    }).success(function(responseData) {
        //do stuff with response
    });