getting null using angular post

2019-09-09 22:15发布

问题:

I'm having some problem using AngularJS post. I keep getting null from the server in my Angular success method even though server returns "success".

Here's my post req:

$http({
            url: "/room/addUserInfo",
            responseType:'json',
            method: "POST",
            data: json,
            headers: {
                "Content-Type": "application/json"
            }
        })
        .success(function(data){
            var a = data;
        })
        .error(function(data){
            var a = data;
        });

And this is my backend (Spring MVC):

@RequestMapping(value="/addUserInfo", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody String addEmployee (@RequestBody User user){
    Database database = new Database(username, pass, dataName, port);
    Connection connection = null;

    try{
        connection = database.connect();
        database.addUserInfo(connection, user);
    }catch(Exception e){
        e.printStackTrace();
        return e.toString();
    }
    return "success";   
}

Just to mention, everything gets stored in the database, so the code is working and server returns "success" it's just that the success method on the client gets null every time. But, when I'm using Angular get method everything works.

Anyone had this kind of problem or knows how to fix this?

Thank you