Search code examples
javascriptjavaangularjsjersey-2.0

fails to send data from cline to server


I try to call from my js to my java backend

angular js (http://localhost:8082/dev/index.html#/):

self.submit = function() {
  console.log("in voice prompt component - submit");
  console.log(self.voice);
  $http.put('localhost:8082/api/Voices/updateVoice', self.voice).then(
    function successCallback(response) {
      self.voices = response.data;
    },
    function errorCallback(response) {
      // called asynchronously if an error occurs
      // or server returns response with an error status.
      console.log(response); 
    });
}

java (should be respond to localhost:8082/api/Voices/updateVoice

@Path("/Voices")
public class VoicesOperation {

...

    @Path("/updateVoice")
    @PUT
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public void updateVoice(VoiceBl voice) throws Exception {

        fillVoicesSnapshot();

I get this js exception:

TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
    at Function.remoteFunction (<anonymous>:3:14)
    at errorCallback (http://localhost:3002/public/js/components/voice-form-component.js:54:29)
    at http://localhost:3002/bower_components/angular/angular.min.js:133:460
    at m.$eval (http://localhost:3002/bower_components/angular/angular.min.js:147:313)
    at m.$digest (http://localhost:3002/bower_components/angular/angular.min.js:144:421)
    at m.$apply (http://localhost:3002/bower_components/angular/angular.min.js:148:78)
    at HTMLButtonElement.<anonymous> (http://localhost:3002/bower_components/angular/angular.min.js:282:247)
    at cg (http://localhost:3002/bower_components/angular/angular.min.js:38:299)
    at HTMLButtonElement.d (http://localhost:3002/bower_components/angular/angular.min.js:38:246)

Solution

  • Problem is with your resource file, you are not sending any thing neither you are consuming. if you are sending a response then your return type should be Response, and also you need to consume.

    @Path("/Voices")
    public class VoicesOperation {
    
    ...
    
        @Path("/updateVoice")
        @PUT
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
        public Response updateVoice(VoiceBl voice) throws Exception {
            Voice voice = fillVoicesSnapshot();
            return Response.status(Status.CREATED)
                           .entity(voice)
                           .build();
        }
    }
    

    or, if you are sending voice type then your return type should be Voice.

    @Path("/Voices")
    public class VoicesOperation {
    
    ...
    
        @Path("/updateVoice")
        @PUT
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
        public Voice updateVoice(VoiceBl voice) throws Exception {
    
            return fillVoicesSnapshot();
        } 
    }