2017-08-07 54 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