我在React中的参数是一个数组,checkedItems:['location1','location2']。调用我的API时,URL如下所示
/api/search?checkedItems[]=location1&checkedItems[]=location2
代替
/api/search?checkedItems=['location1','location2']
我的API应用程序希望查询以逗号分隔的数组参数:
public List<myclass> Get( string checkedItems = null)
{
IQueryable<myclass> qry = (from a in db.myclass
select a);
if (checkedItems != null)
{
string[] items = checkedItems.Split(',');
foreach (var item in items)
{
{
qry = qry.Where(a => items.Contains(a.mycolumn));
}
}
}
return qry.ToList();
}
如何更改反应代码或更改API代码?谢谢。
答案 0 :(得分:1)
代替'/api/search?checkedItems=['location1','location2']'
,用逗号分隔数组字段,就像'/api/search?checkedItems=location1,location2'
为此,
let checkedItems = ['location1', 'location2']
// using template literals
let url = `/api/search?checkedItems=${checkedItems.join(',')}`
然后在后端简单地获取查询参数checkedItems
并将其拆分checkedItems.split(',')
以获得数组。
答案 1 :(得分:0)
这在很大程度上取决于您的后端技术,但是很多的API库/框架会自动为您将参数转换为数组。
请记住,拥有像/api/search?checkedItems=location1&checkedItems=location2
这样的URL(因此:多次使用相同的参数)是完全有效的URI。
我不能说这是否是你的情况。