我是第一次使用AngularJS在MVC中开发Web API,我想知道如何在客户端显示响应消息,向用户显示正在发生的事情。
这是我的代码:
public HttpResponseMessage Delete(int id)
{
Product prouct = new Product { Id = id };
HttpResponseMessage response = new HttpResponseMessage();
if (prouct != null)
{
db.Products.Attach(prouct);
db.Products.Remove(prouct);
if (db.SaveChanges() > 0)
{
response = Request.CreateResponse(HttpStatusCode.OK);
}
else
{
response = Request.CreateResponse(HttpStatusCode.NotFound);
}
}
return response;
}
$scope.DeleteProduct = function (ID) {
if (confirm("Are you sure to delete this product?")) {
$http({
url: 'http://localhost:52795/api/Test',
method: 'DELETE',
params: { Id: ID }
}).success(function (data, xhr, status) {
$scope.GetAllProducts();
console.log(data);
alert(status);
}).error(function (xhr, status) {
console.log(xhr, status);
alert( status);
})
}
}
答案 0 :(得分:0)
您是否尝试使用 $ http()。然后()?
$http({
url: 'http://localhost:52795/api/Test',
method: 'DELETE',
params: { Id: ID }
}).then(function successCallback(response) {
alert(response.data); // This represents the response
}, function errorCallback(response) {
// If some error has occured
});
有关http请求的更多信息,请阅读