简体   繁体   English

AngularJS $ http使用参数中的对象访问ASP.NET Web Api

[英]AngularJS $http get to ASP.NET Web Api with object in parameters

I'm trying to get filtered data from server using a filtering object I pass to the server side. 我正在尝试使用传递给服务器端的筛选对象从服务器获取筛选的数据。 I have managed to get this working with a post: 我设法通过帖子使它工作:

angular: 角度:

var filter: { includeDeleted: true, foo: bar };
$http({ method: 'post', url: 'api/stuff', data: filter });

web api: 网络API:

public IEnumerable<StuffResponse> Post([FromBody]Filter filter)
{
    return GetData(filter);
}

But i don't want to use a post for this, I want to use a get. 但是我不想为此使用帖子,我想使用get。 But this does not work: 但这不起作用:

angular 角度的

$http({ method: 'get', url: 'api/stuff', params: filter });

web api 网络API

public IEnumerable<StuffResponse> Get([FromUri]Filter filter)
{
    return GetData(filter);
}

Also tried specifying params: { filter: filter }. 还尝试指定参数:{filter:filter}。 If i try [FromBody] or nothing, filter is null. 如果我尝试[FromBody]或不尝试,则filter为null。 With the FromUri i get an object at least - but with no data. 使用FromUri,我至少可以得到一个对象-但没有数据。 Any ideas how to solve this, without creating input parameters for all filter properties? 有什么想法可以解决此问题,而无需为所有过滤器属性创建输入参数?

Yes you can send data with Get method by sending them with params option 是的,您可以通过使用params选项发送数据来使用Get方法发送数据

var data ={
  property1:value1,
  property2:value2,
  property3:value3
};

$http({ method: 'GET', url: 'api/controller/method', params: data });

and you will receive this by using [FromUri] in your api controller method 并且您将通过在api控制器方法中使用[FromUri]收到此信息

public IEnumerable<StuffResponse> Get([FromUri]Filter filter)
{
    return GetData(filter);
}

and the request url will be like this 并且请求网址将如下所示

http://localhost/api/controller/method?property1=value1&property2=value2&property3=value3

Solved it this way: 这样解决:

Angular: 角度:

$http({
       url: '/myApiUrl',
       method: 'GET',
       params: { param1: angular.toJson(myComplexObject, false) }
      })

C#: C#:

[HttpGet]
public string Get(string param1)
{
     Type1 obj = new JavaScriptSerializer().Deserialize<Type1>(param1);
     ...
}

A HTTP GET request can't contain data to be posted to the server. HTTP GET请求不能包含要发布到服务器的数据。 What you want is to aa query string to the request. 您想要的是对请求的查询字符串。 Fortunately angular.http provides an option for it params . 幸运的是, angular.http为它提供了一个params选项。

See : http://docs.angularjs.org/api/ng/service/ $http#get 参见:http: //docs.angularjs.org/api/ng/service/ $ http#get

you can send object with Get or Post method . 您可以使用Get或Post方法发送对象。

Script 脚本

//1.
var order = {
     CustomerName: 'MS' };
//2.
var itemDetails = [
     { ItemName: 'Desktop', Quantity: 10, UnitPrice: 45000 },
     { ItemName: 'Laptop', Quantity: 30, UnitPrice: 80000 },
     { ItemName: 'Router', Quantity: 50, UnitPrice: 5000 }
];
//3.
$.ajax({
     url: 'http://localhost:32261/api/HotelBooking/List',
     type: 'POST',
     data: {order: order,itemDetails: itemDetails},
     ContentType: 'application/json;utf-8',
     datatype: 'json'
    }).done(function (resp) {
        alert("Successful " + resp);
    }).error(function (err) {
        alert("Error " + err.status);});

API Code API代码

[Route("api/HotelBooking/List")]
[HttpPost]
public IHttpActionResult PostList(JObject objData)
{  List<ItemDetails > lstItemDetails = new List<ItemDetails >();
   dynamic jsonData = objData;
   JObject orderJson = jsonData.itemDetails;
   return Ok();}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM