繁体   English   中英

Javascript Ajax将动态创建的json对象传递给Web服务

[英]Javascript ajax passing dynamically created json object to a web service

我有一个带有对象参数的网络服务功能,

控制器功能

 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为空,但是当我从ajax检查我的通过值是否有价值时,请从Google chrome控制台检查它

来自ajax jsonObj: { AccountName:"test name", AccountAddress: "test address", etc.. }的值的示例格式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) {

                });

            });

但是当我尝试

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

它有效,并且成功地将期望值传递给函数,我的示例只有2个对象,但是在我的真实代码中,我有40多个对象,这就是为什么我尝试使用loop ..任何人都知道如何解决该问题?

谢谢

附加代码,以填充我的表单

$.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);
            });

我在您的代码中发现了几个错误:

  • 首先,您使用jsonObj [this.id]为对象成员分配值。 因此this.id应该为AccountNameAccountNumber,否则它将不会为所需成员分配值。
  • 其次,从JSON.stringify中删除多余的刹车{}并像这样使用

    JSON.stringify(jsonObj);

解决问题的方式

data: JSON.stringify(jsonObject)

谢谢你们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM