我想將對象傳遞給我的API,但它始終在API中顯示對象爲NULL。通過AJAX將對象傳遞給API MVC C#
我的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
顯示爲空,但在發送之前,我可以看到數據在那裏...?我從外部API調用得到這個數據,並從響應得到它:
var tranData = JSON.parse(req.response);
看到這個[如何通過JSON POST數據爲對象的Web API方法(https://stackoverflow.com/questions/20226169/how-to-pass-json -post-data-to-web-api-method-as-object/20226220#20226220) – Shyju
[如何將json POST數據傳遞給Web API方法作爲對象](https://stackoverflow.com/questions/ 20226169 /如何對傳遞JSON-後數據至web的API-方法-作爲對象) – Munzer