在MVC中通过Ajax发送多个数组

时间:2022-11-16 21:30:48

I am trying to build an API with 1 string and 5 arrays as parameters:

我正在尝试使用1个字符串和5个数组作为参数构建API:

public List<HomeClass> GetData(string id, string[] price, string[] type, string[] sqft, string[] beds, string[] baths)
{
}

I am using jQuery to call this API like so:

我正在使用jQuery来调用这个API,如下所示:

var priceArray = [];
    var typeArray = [];
    var sqftArray = [];
    var bedroomsArray = [];
    var bathroomsArray = [];

function searchInventory(community, price, type, sqft, bedrooms, bathrooms) {

$.get('/api/HomesAPI/' + community + '/' + price + '/' + type + '/' + sqft + '/' + bedrooms + '/' + bathrooms).then(function (result) {

});

}

searchInventory(getURLParameter(window.location.pathname.split('/'))[2], priceArray, typeArray, sqftArray, bedroomsArray, bathroomsArray);

So I am passing 1 string and 5 arrays, but my API is not being called, if I change the array to strings in both .NET and jQuery, it works just fine, but not with arrays. What am I doing wrong?

所以我传递了1个字符串和5个数组,但是我的API没有被调用,如果我将数组更改为.NET和jQuery中的字符串,它可以正常工作,但不适用于数组。我究竟做错了什么?

Here is my route

这是我的路线

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}/{price}/{type}/{sqft}/{beds}/{baths}",
                defaults: new { id = RouteParameter.Optional }
            ); 

1 个解决方案

#1


4  

You should use $.param method, in order to serialize your object.

您应该使用$ .param方法,以序列化您的对象。

$.param method create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request.

$ .param方法创建适合在URL查询字符串或Ajax请求中使用的数组,普通对象或jQuery对象的序列化表示。

$.get('/api/HomesAPI',$.param({id:community, price:price, type:type, sqft:sqft, beds:bedrooms , baths:bathrooms },true),function (data) {

});

or you can pass an object to server.

或者您可以将对象传递给服务器。

Also, you have to use traditional:true property in order to use the traditional style of parameters serialization.

此外,您必须使用传统的:true属性才能使用传统的参数序列化方式。

var data = {
   id: community,
   price:price,
   type:type,
   sqft:sqft,
   beds:bedrooms,
   baths:bathrooms
};

Ajax

阿贾克斯

$.ajax({
    url: '/api/HomesAPI',
    type: "GET",
    data:data,
    traditional:true
});

Note: You do not need to use another route.

注意:您不需要使用其他路线。

#1


4  

You should use $.param method, in order to serialize your object.

您应该使用$ .param方法,以序列化您的对象。

$.param method create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request.

$ .param方法创建适合在URL查询字符串或Ajax请求中使用的数组,普通对象或jQuery对象的序列化表示。

$.get('/api/HomesAPI',$.param({id:community, price:price, type:type, sqft:sqft, beds:bedrooms , baths:bathrooms },true),function (data) {

});

or you can pass an object to server.

或者您可以将对象传递给服务器。

Also, you have to use traditional:true property in order to use the traditional style of parameters serialization.

此外,您必须使用传统的:true属性才能使用传统的参数序列化方式。

var data = {
   id: community,
   price:price,
   type:type,
   sqft:sqft,
   beds:bedrooms,
   baths:bathrooms
};

Ajax

阿贾克斯

$.ajax({
    url: '/api/HomesAPI',
    type: "GET",
    data:data,
    traditional:true
});

Note: You do not need to use another route.

注意:您不需要使用其他路线。