use*_*582 13 javascript c# ajax asp.net-web-api
我有一个web api控制器,我想发布两个参数.一个是扁平的int ID,另一个是IDictionary或类似的等价物.
[HttpPost]
public void DoStuff(int id, [FromBody]IDictionary<int, int> things)
{
}
var things = new Array();
things.push({ 1: 10 }); // tried things.push({ Key: 1, Value: 10 })
things.push({ 2: 11 });
$.ajax({
url: '/api/DoStuff?id=' + id,
data: '=' + JSON.stringify(things), // tried what seems like 100 different things here
type: 'POST',
dataType: 'json'
});
Run Code Online (Sandbox Code Playgroud)
无论我在数据参数(data: things
,data: '=' + things
)中尝试什么,字典都不会通过api控制器.它为null或有一个伪造条目({0,0}).
我也尝试在Uri中发送词典 - 不用了.
如何将字典或键值对发送到api控制器?
car*_*ira 18
您不需要数组 - 您需要一个简单的JS对象(映射到字典).这段代码应该有效:
var things = {};
things['1'] = 10;
things['2'] = 11;
$.ajax({
url: '/api/DoStuff?id=' + id,
data: JSON.stringify(things),
contentType: 'application/json'
type: 'POST',
dataType: 'json'
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28786 次 |
最近记录: |