locked
Ajax call to execute WebApi method that is returning complex object. RRS feed

  • Question

  • User1959606010 posted

    I have a Ajax call to a WebApi method. My Call is looks like this:

     function CalculateAmortizationScheduleAPI()
        {
    
            $.ajax({
                url: 'http://localhost:62815/v1/APR/Calculate',
                type: 'POST',
                dataType: 'json',
                    data: {
                        "LoanAmount": 200000,
                        "Costs": 100,
                        "Term": 15,
                        "Rate": 2.2,
                        "RateType": 2,
                        "Points": 1,
                        "MIRate": 2.2,
                        "UFMIRate": 2,
                        "FixedTerm": 5,
                        "FirstPaymentDate": "10/10/2017",
                        "RateChangeDate": "10/10/2018",
                        "AdjustTerm": 2,
                        "IndexRate": 2,
                        "Margin": 2,
                        "AdjustmentCapFirst": 2,
                        "AdjustmentCap": 2,
                        "AdjustmentCapLifetime": 2,
                        "EstimatedPropertyValue": 2,
                        "CancelMIPMonths": 2,
                        "CancelMIPLTV": 2,
                        "AdditionalPricipalPayment": 2,
                        "ConstantPaymentValue": 2
                    },
                    success: function (data) {
                        alert("success");
    
                },
                error: function () {
                    alert("Error");
                },
            });
        }

    And this is my WebApi method that Ajax call successfully hit when I put the breakpoints and also the return value has value as a object.

         [HttpPost]
         public Dictionary<int, AmItem> Calculate([FromBody]AmArguments 
         lAmArguments)
        {
    
         .....
    
         return AmSchedule;
          }

    The problem is that even though the Ajax call hit the method but the Success method is not executing and the Error Alert will appear in the screen, Also I don't know how to capture return value that is complex object (AmSchedule)

    Friday, May 19, 2017 6:12 PM

All replies

  • User-2057865890 posted

    Hi nikoo56,

    The problem is that even though the Ajax call hit the method but the Success method is not executing and the Error Alert will appear in the screen, Also I don't know how to capture return value that is complex object (AmSchedule)

    Using the F12 developer tools, the Network tool gives you the fine details of any network requests involved in the loading and operation of your webpages. Debugging AJAX requests by viewing request and response headers and bodies.

    Test code

    [HttpPost]
    public Dictionary<int, AmItem> Calculate([FromBody]AmArguments lAmArguments)
    {
        var AmSchedule = new Dictionary<int, AmItem>
        {
            { 1, new AmItem { MyProperty = 1 } },
            { 2, new AmItem { MyProperty = 2 } },
            { 3, new AmItem { MyProperty = 3 } }
        };
        return AmSchedule;
    }

    Model

    public class AmArguments
    {
        public int LoanAmount { get; set; }
        public int Costs { get; set; }
        public int Term { get; set; }
        public decimal Rate { get; set; }
        public int RateType { get; set; }
        public int Points { get; set; }
        public decimal MIRate { get; set; }
        public int UFMIRate { get; set; }
        public int FixedTerm { get; set; }
        public DateTime FirstPaymentDate { get; set; }
        public DateTime RateChangeDate { get; set; }
        public int AdjustTerm { get; set; }
        public int IndexRate { get; set; }
        public int Margin { get; set; }
        public int AdjustmentCapFirst { get; set; }
        public int AdjustmentCap { get; set; }
        public int AdjustmentCapLifetime { get; set; }
        public int EstimatedPropertyValue { get; set; }
        public int CancelMIPMonths { get; set; }
        public int CancelMIPLTV { get; set; }
        public int AdditionalPricipalPayment { get; set; }
        public int ConstantPaymentValue { get; set; }
    }
    
    public class AmItem
    {
        public int MyProperty { get; set; }
    }

    Best Regards,

    Chris

    Monday, May 22, 2017 7:16 AM