2016-02-22 28 views
0

我有一个AngularJS web应用程序调用我自己的Java Jersey API。现在一切都在localhost上运行。POST 500(内部服务器错误) - AngularJS - Jersey API

这是角电话:

function Create(user) { 
     return $http.post('http://localhost:8080/NobelGrid/api/users/create/', user).then(handleSuccess, handleError('Error creating user')); 
} 

这是REST的我的代码片段:

@Path("/create") 
@POST 
@Produces("application/json") 
public Response create(String data) { 

    UserDataConnector connector; 
    JSONObject response = new JSONObject(data); 

    User userToCreate = new User(response.getString("surname"), response.getString("name"), 
      response.getString("mail"), response.getString("username"), response.getString("password"), 0); 

    try { 

     connector = new UserDataConnector(); 
     connector.createUser(userToCreate); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return Response.status(Response.Status.OK) // 200 
      .entity(userToCreate) 
      .build(); 

} 

我简单地插入我自己的MySQL数据库的用户。

程序进行得很顺利导致用户被添加到Db中,无论如何它返回给我那个错误。

enter image description here

enter image description here

请谁能帮助我?

+4

500表示请求处理期间服务器端出现问题。所以添加你的服务器错误日志。 – mmuzahid

+0

@mmuzahid我知道这一点。但正如我所说,在服务器端看起来一切都很顺利:用户被添加到BD。我在调试模式下通过指令执行所有的服务和指令,并且没有发生异常。 –

+1

为什么额外的'/'在URL中的'/ create'后面?我认为它应该是:'http:// localhost:8080/NobelGrid/api/users/create' –

回答

0

试试这个:

function Create(user) { 
    return $http({ 
      'url':'/NobelGrid/api/users/create', 
      'method': POST, 
      'data': user 
      }).then(function() { 
      // ..... 
      }); 
} 
+0

好的。关键字'方法'不''''。我在帖子中的原始问题也有同样的错误。 :/ –

0

$http.post('http://localhost:8080/NobelGrid/api/users/create/', user).then(handleSuccess, handleError('Error creating user'));

var config = { 
     headers: { 'Content-Type': 'application/x-www-form-urlencoded'} 
}; 

$http.post('http://localhost:8080/NobelGrid/api/users/create/', $httpParamSerializer(user), config).then(handleSuccess, handleError('Error creating user')); 

你需要注入$ httpParamSerializer。