如何知道web api中的服务器响应

时间:2016-03-10 18:00:15

标签: angularjs asp.net-mvc web-services

我是第一次使用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);
        })
    }
}

1 个答案:

答案 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请求的更多信息,请阅读

https://docs.angularjs.org/api/ng/service/ $ HTTP