2017-08-31 81 views
0

我想將對象傳遞給我的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); 
+2

看到這個[如何通過JSON POST數據爲對象的Web API方法(https://stackoverflow.com/questions/20226169/how-to-pass-json -post-data-to-web-api-method-as-object/20226220#20226220) – Shyju

+0

[如何將json POST數據傳遞給Web API方法作爲對象](https://stackoverflow.com/questions/ 20226169 /如何對傳遞JSON-後數據至web的API-方法-作爲對象) – Munzer

回答

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 
    .... 
} 
0

首先創建一個類 ID,帳戶ID,的clientId,的agentId等你逝去的

下一頁創建對象類

[HttpGet] 
    [Route("Api/ReportApi/SummaryReport")] 
    public IHttpActionResult SummaryReport(Class classobj) 
    { your code here } 

後,在阿賈克斯 你需要發送JSON數據的格式

像tranData {{ 「ID」:16, 「帳戶ID」:11111000010,「CLI entId「:1等等}