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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22090431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:24:45  来源:igfitidea点击:

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

javascriptasp.netangularjsasp.net-web-api

提问by Trylling

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:

网络接口:

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. But this does not work:

但我不想为此使用帖子,我想使用 get。但这不起作用:

angular

有角的

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

web api

网络接口

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

Also tried specifying params: { filter: filter }. If i try [FromBody] or nothing, filter is null. With the FromUri i get an object at least - but with no data. Any ideas how to solve this, without creating input parameters for all filter properties?

还尝试指定参数:{过滤器:过滤器}。如果我尝试 [FromBody] 或什么都不尝试,则过滤器为空。使用 FromUri 我至少得到一个对象 - 但没有数据。任何想法如何解决这个问题,而无需为所有过滤器属性创建输入参数?

采纳答案by Ramesh Rajendran

A HTTP GETrequest can't contain data to be posted to the server. What you want is to a a query string to the request. Fortunately angular.httpprovides an option for it params.

一个HTTP GET不能包含数据请求被发送到服务器。您想要的是请求的查询字符串。幸运的是angular.http为它提供了一个选项params

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

请参阅:http : //docs.angularjs.org/api/ng/service/$http#get

回答by Ibrahim Kais Ibrahim

Yes you can send data with Getmethod by sending them with paramsoption

是的,您可以通过使用 选项发送数据来使用Get方法发送数据params

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

并且您将通过[FromUri]在您的 api 控制器方法中使用来接收它

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

回答by Vil

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);
     ...
}

回答by sanjeewa

you can send object with Get or Postmethod .

您可以使用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

接口代码

[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();}