我正在尝试将一个对象传递给我的API,但它一直在API中将对象显示为NULL。
我的Ajax电话:
$.ajax({
type: "GET",
url: url,
data: tranData,
dataType: "json",
success: function(response){
var itemcount = response.itemsCount;
var dataReturned = response.data;
}});
我的API方法:
[HttpGet]
[Route("Api/ReportApi/SummaryReport")]
public IHttpActionResult SummaryReport(JObject tranData)
{
dynamic oData = tranData;
BankTransactionsViewModelResults transactions = new BankTransactionsViewModelResults();
transactions = oData;
var summarisedTransactions = SummariseResults(transactions.BankTransactionsViewModelList, "Test Ref");
var response = new
{
data = summarisedTransactions,
itemsCount = summarisedTransactions.Count(),
};
return Request.GetOKRequest(response);
}
我想传递的对象:
[{"id":16,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10a","description":"TEST10d","amount":600000.0,"balance":600000.0,"tranDate":1494374400000},
{"id":17,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"WITHDRAWAL","reference":"TEST10b","description":"TEST10d","amount":-400000.0,"balance":200000.0,"tranDate":1494460800000},
{"id":18,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10c","description":"TEST10c","amount":700000.0,"balance":900000.0,"tranDate":1494547200000},
{"id":19,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"TRANSFER","reference":"TEST10d","description":"TEST10d","amount":-600000.0,"balance":300000.0,"tranDate":1494633600000},
{"id":20,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10e","description":"TEST10e","amount":800000.0,"balance":1100000.0,"tranDate":1494720000000},
{"id":21,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"WITHDRAWAL","reference":"TEST10f","description":"TEST10f","amount":-800000.0,"balance":300000.0,"tranDate":1494806400000},
{"id":22,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10g","description":"TEST10g","amount":900000.0,"balance":1.2E7,"tranDate":1494892800000},
{"id":23,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"TRANSFER","reference":"TEST10h","description":"TEST10h","amount":-400000.0,"balance":800000.0,"tranDate":1494979200000},
{"id":24,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10i","description":"TEST10i","amount":800000.0,"balance":1.6E7,"tranDate":1495065600000},
{"id":25,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"TRANSFER","reference":"TEST10j","description":"TEST10j","amount":-800000.0,"balance":800000.0,"tranDate":1495152000000},
{"id":26,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10k","description":"TEST10k","amount":900000.0,"balance":1.7E7,"tranDate":1495238400000},
{"id":27,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"WITHDRAWAL","reference":"TEST10l","description":"TEST10l","amount":-700000.1,"balance":0.0,"tranDate":1495324800000}]
如何将此传递给我的API? tranData
显示为null但在发送之前我可以看到数据在那里......?我从外部API调用中获取此数据并从响应中获取:
var tranData = JSON.parse(req.response);
答案 0 :(得分:1)
如果您想使用HttpGet
来完成请求,则必须使用此StackOverflow question中提到的严格类型对象。您可以将方法签名更改为以下
[HttpGet]
[Route("Api/ReportApi/SummaryReport")]
public IHttpActionResult SummaryReport([FromUri]YourCustomClass tranData)
{
// your code
}
更好的选择是使用HttpPost
。
在这种情况下,您必须使用JToken
而不是JObject
。以下是您的签名的样子
[HttpPost]
[Route("Api/ReportApi/SummaryReport")]
public IHttpActionResult SummaryReport([FromBody]JToken tranData)
{
BankTransactionsViewModelResults transactions = tranData.ToObject<BankTransactionsViewModelResults>();
// other processing code
....
}
答案 1 :(得分:0)
首先创建一个类 id,accountId,clientId,agentId等等你传递的内容
接下来创建类
的对象[HttpGet]
[Route("Api/ReportApi/SummaryReport")]
public IHttpActionResult SummaryReport(Class classobj)
{ your code here }
在ajax之后你需要发送json数据格式
与tranData {{“id”:16,“accountId”:11111000010,“clientId”:1之类似}}