2017-08-07 51 views
0

我有一個具有一個對象參數的web服務的功能,從控制器的Javascript AJAX通過動態創建JSON對象到Web服務

public string Post([FromBody]LoanApplication value) 
      { 
       LoanApplicationDAO appDAO = new LoanApplicationDAO(); 
       string res = ""; 
       res = appDAO.ReleaseLoanApplication(value); 
       if (res == null) 
       { 
        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); 
       } 
       return res; 
      } 

LoanApplication

函數包含

public class LoanApplication 
    { 
     public string AccountAddress { get; set; } 
     public string AccountName { get; set; } 
     public string AccountNumber { get; set; } 
     public string AccountTag { get; set; } 
     public string ApplicationNumber { get; set; } 
     public string ApplicationType { get; set; } 
     public string Approver { get; set; } 
     public string BSPTagging { get; set; } 
     public string BuyOutAmount { get; set; } 
     public string CIFKey { get; set; } 
     public string ClassificationEconomicActivity { get; set; } 
     public string ClassificationSizeOfFirm { get; set; } 
     public string CoMaker1 { get; set; } 
     public string CoMaker2 { get; set; } 
     public string CoMakerName1 { get; set; } 
     public string CoMakerName2 { get; set; } 

     public string CreditLimit { get; set; } 
     public string DateGranted { get; set; } 
     public string DepEdDivision { get; set; } 

     public string DepEdEmployeeID { get; set; } 
     public string DepEdRegion { get; set; } 
     public string DepEdStation { get; set; } 
     public string Disbursement { get; set; } 
     public string DocStamps { get; set; } 
     public string DOSRIField { get; set; } 
     public string EmailAddress { get; set; } 
     public string FirstPaymentDate { get; set; } 
     public string GroupCode { get; set; } 
     public string GroupName { get; set; } 
     public string Insurance { get; set; } 
     public string InterestRate { get; set; } 
     public string KnockedOffAccountNumber { get; set; } 
     public string KnockedOffAmount { get; set; } 
     public string LandlineNumber { get; set; } 
     public string LoanAmount { get; set; } 
     public string LPOCode { get; set; } 
     public string Maker { get; set; } 
     public string MaturityDate { get; set; } 
     public string MobileNumber { get; set; } 
     public string MonthlyAmort { get; set; } 
     public string MothersMaidenName { get; set; } 
     public string NDaysDiscount { get; set; } 
     public string NoOfInstall { get; set; } 
     public string PaymentFrequency { get; set; } 
     public string PayOutMode { get; set; } 
     public string PEPTagging { get; set; } 
     public string Product { get; set; } 
     public string Purpose { get; set; } 
     public string Security { get; set; } 
     public string ServiceFees { get; set; } 
     public string SourceOfPayment { get; set; } 
     public string SpouseMobileNumber { get; set; } 
     public string SpouseName { get; set; } 
     public string Term { get; set; } 
     public string AOUserID { get; set; } 
     public string AOName { get; set; } 
     public string LSOCode { get; set; } 
     public string IsBranch { get; set; } 
} 

當我使用調試模式從VS 2012 LoanObj accountname and accountnumer爲空,但是當我從阿賈克斯檢查我的通行證價值它的價值,檢查了它從谷歌鉻合金ç從阿賈克斯jsonObj: { AccountName:"test name", AccountAddress: "test address", etc.. }

我的AJAX功能

$('body').on('click', '#btnSubmit', function() { 
       var jsonObj = {}; 
       $('#lfs_form tbody input[type="text"]').each(function() { 
        jsonObj[this.id] = this.value; 
       }); 
       var req2 = 
        $.ajax({ 
        type: 'post', 
        url: '../lfsapi/loanapplication/', 
        contentType: 'application/json; charset=utf-8', 
        dataType: 'json', 
        data: JSON.stringify({ 
         jsonObj 
         //AccountAddress: jsonObj['AccountAddress'] 
        }) 
       }); 

       req.error(function (request, status, error) { 
        alert(request.responseJSON['Message']); 
       }); 

       req.done(function (data) { 

       }); 

      }); 

但價值onsole

樣本格式,當我嘗試

data: JSON.stringify({ 
AccountName: jsonObj['AccountName'], 
AccountNumber: jsonObj['AccountNumber'] 
}) 

它的工作原理,並順利通過預期值的功能,我的樣本不僅是2個對象,但在我真正的代碼,我有超過40級的對象就是爲什麼我使用loop..anyone試圖知道我該如何解決這個問題?

謝謝

附加代碼,來填充自己的狀態

$.ajax({ 
       type: 'get', 
       url: '../lfsapi/loanapplication/', 
       contentType: 'application/json; charset=utf-8', 
       dataType: 'json' 
      }); 

      req.error(function (request, status, error) { 
       alert(request.responseJSON['Message']); 
      }); 

      req.done(function (data) { 
       var toappend = ''; 
       $.each(data, function (key, val) { 
        toappend += '<tr>'; 
        toappend += '<td>' + val + '</td><td><input style="width:500px;" type="text" id=' + val + ' /></td>'; 
        toappend += '</tr>'; 
       }); 
       toappend += '<tr><td align="right" colspan="2"><button id="btnSubmit" type="button">Submit</button></td></tr>'; 
       $('#lfs_form tbody').append(toappend); 
      }); 
+1

'SyntaxError'。 'data:JSON.stringify({jsonObject})'。 'jsonObject'已經是一個對象了,你基本上調用'JSON.stringify({{}});'。刪除多餘的括號... – Crowes

+0

也可嘗試檢查是否有jsonObj任何值,看起來像你的選擇是不正確的'$(「#輸入[類型=‘文本’]」)'你有ID爲許多輸入輸入' '?但正如@Crowes說,開始去除多餘的'{}''從調用JSON.stringify' – nicowernli

+0

@Crowes ,,金正日更新我的代碼,它只是錯字error..thanks – sdu9

回答

0

通過

data: JSON.stringify(jsonObject) 

解決問題謝謝大家

0

有幾個錯誤我在你的代碼注意:

  • 首先使用的是jsonObj [this.id ]用於分配值到對象 成員。所以this.id應該是帳戶名稱帳戶編號否則它 不會將值分配給所需的成員。
  • 其次,從JSON.stringify刪除多餘brakets {}和使用這樣

    JSON.stringify( jsonObj );

+0

虐待更新我的代碼先生,它只是錯字error..thanks – sdu9