为什么这个angularJS代码不起作用?

时间:2016-12-07 14:42:23

标签: mysql angularjs rest

我创建了使用RESTful服务的angularJS代码,将一些表单数据插入到MySql数据库中。当我提交表单数据时,没有回复。这段代码中的错误是什么?

显示输出: enter image description here

它在Eclipse控制台上显示了一些错误消息,如下所示:

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
    at ang.CourseService.addC(CourseService.java:39)
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jersey-serlvet] in context with path [/AngularDemo] threw exception
java.lang.NullPointerException
    at ang.CourseService.addC(CourseService.java:39)

这是我的angularJS代码:

var postApp = angular.module('postApp', []);
// Controller function and passing $http service and $scope var.
postApp.controller('postController', function($scope, $http) {
  // create a blank object to handle form data.
    $scope.user = {};
  // calling our submit function.
    $scope.submitForm = function() {
    // Posting data to REST
    $http({
      method:'POST',
      url:'http://localhost:8080/AngularDemo/rest/courses/addNew',
      data:$scope.user,
      headers:{'Content-Type': 'application/x-www-form-urlencoded'}
      //data:angular.toJson($scope.user),
      //headers:{'Content-Type' : 'application/json'} 
     })
      .success(function(data) {
        if (data.errors) {
          // Showing errors.
          $scope.errorid = data.errors.id;
          $scope.errorname = data.errors.name;
          $scope.errorduration = data.errors.duration;
          $scope.errorfee = data.errors.fee;
        } else {
          $scope.message = data.message;
        }
      });
    };
});

这是我的REST服务:

@POST
@Path("/addNew")
@Produces(MediaType.APPLICATION_JSON)
public void addC(@FormParam("id") int i,
        @FormParam("name") String n,
        @FormParam("duration") String d,
        @FormParam("fee") Double f)throws Exception
 { 
    CourseController controller=new CourseController();
    Course c=new Course();
    c.setId(i);
    c.setName(n);
    c.setDuration(d);
    c.setFee(f);
    controller.addCourse(c); 
}

0 个答案:

没有答案