Angular JS $http 请求网络服务
Angular JS $http request to webservice
我想制作一个 AngularJS $http 请求函数,它将从服务器端调用此方法(使用 JAX-RS Jersey),此方法使用 JSON 作为参数和 returns 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();
}
假设 Web 服务的 URL 是“http://localhost:8084/iec3/rest/user/login”,您能否举例说明 AngularJS 中的 $http
请求函数?
您可以像下面的代码片段一样向服务器发出请求:
$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);
});
};
我想制作一个 AngularJS $http 请求函数,它将从服务器端调用此方法(使用 JAX-RS Jersey),此方法使用 JSON 作为参数和 returns 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();
}
假设 Web 服务的 URL 是“http://localhost:8084/iec3/rest/user/login”,您能否举例说明 AngularJS 中的 $http
请求函数?
您可以像下面的代码片段一样向服务器发出请求:
$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);
});
};