在AngularJS中,可以使用$http服务来发起HTTP请求并处理响应。下面是一个包含代码示例的解决方法:
$http.get('/api/users')
.then(function(response) {
// 处理成功响应
console.log(response.data);
}, function(error) {
// 处理错误响应
console.log(error);
});
var user = { name: 'John', email: 'john@example.com' };
$http.post('/api/users', user)
.then(function(response) {
// 处理成功响应
console.log(response.data);
}, function(error) {
// 处理错误响应
console.log(error);
});
var user = { name: 'John', email: 'john@example.com' };
$http.put('/api/users/1', user)
.then(function(response) {
// 处理成功响应
console.log(response.data);
}, function(error) {
// 处理错误响应
console.log(error);
});
$http.delete('/api/users/1')
.then(function(response) {
// 处理成功响应
console.log(response.data);
}, function(error) {
// 处理错误响应
console.log(error);
});
这些示例展示了如何使用$http服务发起不同类型的HTTP请求,并使用.then()方法处理成功和错误的响应。在成功回调函数中,可以通过response.data访问响应的数据。在错误回调函数中,可以通过error访问错误信息。