对webservice

时间:2015-12-30 13:07:54

标签: angularjs web-services rest jersey jax-rs

我想创建一个AngularJS $ http请求函数,它将从服务器端调用此方法(使用JAX-RS Jersey),此方法为参数使用JSON并返回JSON,

@Path("/login")
@POST
@Consumes(value = MediaType.APPLICATION_JSON)
@Produces(value = MediaType.APPLICATION_JSON)
public Response login(User user) {
    UserDao ud = new UserDao();
    User dariDB = ud.login(user.getUsername(), user.getPassword());
    dariDB.setPassword("");
    return Response.ok(dariDB)
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
            .header("Access-Control-Allow-Credentials", "true")
            .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
            .header("Access-Control-Max-Age", "1209600")
            .build();
}

您能否通过给AngularJS中的$http请求函数提供示例来帮助我,假设Web服务的URL是" http://localhost:8084/iec3/rest/user/login" ?

1 个答案:

答案 0 :(得分:2)

您可以向服务器发出请求,如下面的代码段:

$http({
        method: 'POST',
        url: 'http://localhost:8084/iec3/rest/user/login'
        data: user
    })

控制器的完整示例如下:

$scope.login = function() {
    $http({
        method: 'POST',
        url: 'http://localhost:8084/iec3/rest/user/login'
        data: user
    }).then(function(response) {
        console.log(response);
    }, function(error) {
        console.log(error);
    });
};