nik
nik

Reputation: 564

Ajax call to WebAPI goes to Error instead of success

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.

Upvotes: 0

Views: 1273

Answers (1)

Asif Raza
Asif Raza

Reputation: 1021

dataType: 'json',

Its mean return type of data from server should be json

Make sure are you returning json object to ajax success call back - if not then you need to change it to dataType: 'text',

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead More detial please visit - Visit

I hope this will work fine for you - still you facing error please comment below

Upvotes: 1

Related Questions