2016-04-24 112 views
0

我試圖刪除對象,並返回其不觸發網頁API控制方法的列表,但然後得到錯誤

Expected response to contain an object but got an array (Request: DELETE 



$scope.deleteProduct = function (productId) { 
     productResource.delete({ 
      id: productId 
     }, function (data) { 
      $scope.products = data; 
     }); 
    } 

資源控制器

function productResource($resource) { 


    return $resource("/api/products/:id"); 
    } 

的Web API控制器

public IQueryable Delete(int id) 
    { 
     var repository = new ProductRepository(); 
     return repository.Delete(id).AsQueryable(); 

    } 

這是CA將返回到產品列表的數據庫。

internal List<Product> Delete(int Id) 
    { 
     IDbConnection connection; 
     using (connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Liberty"].ToString())) 
     { 
      var result = connection.QueryMultiple("DeleteProduct", new{prodId = Id}, commandType: CommandType.StoredProcedure); 
      var products = result.Read<Product>().ToList(); 
      return products; 
     } 
    } 

我該如何解決這個錯誤的方法?

回答

1

您可以指定DELETE操作的返回類型爲數組,因爲這是你的Web API控制器返回:

function productResource($resource) { 
    return $resource("/api/products/:id", { }, { 
     'delete': { 
      method: 'DELETE', 
      isArray: true 
     } 
    }); 
}