3

I have a method in MVC that I post to it and I need to return some data back to process. This is my MVC method that I post to and return Value is a json data.

[HttpPost]
public JsonResult GetCalculateAmortizationSchedule()
{
    var data = ...
    var httpClient = new HttpClient();
    var response = httpClient.PostAsJsonAsync("http://localhost:62815/v1/APR/CalculateAmortizationSchedule", data).Result;
    var returnValue = response.Content.ReadAsAsync<Dictionary<int, AmItem>>().Result;
    return Json(returnValue);
}

This is my AJax call that successfully run the MVC method.

$('#MyForm').submit(function (e) {
    debugger;
    $.ajax({
        url: "/home/GetCalculateAmortizationSchedule",
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",                
        success: function (result) {
            alert("success");
        },
        error: function (result) {
            alert("error");
        }
    });
    e.preventDefault();
});

My problem is how to catch the return value from method? when I run this after return Json(returnValue) the value is not returning to Ajax method and instead of seeing the Success Alert I see the error Alert.

rolspace
  • 335
  • 1
  • 5
  • 11
nik
  • 514
  • 2
  • 6
  • 19
  • 1
    Try alerting your result to see what data you get back. There should be some kind of message that helps you debug So instead of alert("error") do alert(result) – garek007 May 22 '17 at 23:19
  • **developer** tools console or network tab will show you what is happening and should be your first port of call for debugging – Jaromanda X May 22 '17 at 23:20
  • @garek007 it is [object Object] – nik May 22 '17 at 23:20
  • use `console.log()` – Junius L May 22 '17 at 23:22
  • ok change your error to this error: (xhr,status,error) then in the function do console.log(xhr), console.log(status), console.log(error) – garek007 May 22 '17 at 23:23
  • That will give you even more detail, but if it's erroring out, it's something to do with your PHP code I imagine. What framework are you using? – garek007 May 22 '17 at 23:23
  • @garek007 MVC in .net framework – nik May 22 '17 at 23:24
  • Ok I can't help with .NET but if you add that to your error and console log it, you'll be able to get more info – garek007 May 22 '17 at 23:25
  • Did you have any luck? – garek007 May 23 '17 at 01:24
  • result in success(result) is your return value. – Allen King May 23 '17 at 03:53
  • You need to loop over it , $.each jquery method. – Asif Raza May 23 '17 at 05:19
  • example. $.each(result, function (i, data) { var row = data; console.log(row); }); – Asif Raza May 23 '17 at 05:27
  • @AsifRaza I am getting this error in row: System.ArgumentException: Type 'System.Collections.Generic.Dictionary is not supported for serialization/deserialization of a dictionary, keys must be strings or objects. I think it is realted to my return value that is dictionary type in this line: var returnValue = response.Content.ReadAsAsync>().Result; – nik May 23 '17 at 15:38
  • Can you put break point on return Json(returnValue); ? Are you getting value as Dictionary Way ? key - value pair ... & then you need convert dictionary collection object to json string – Asif Raza May 23 '17 at 16:42

3 Answers3

3

Try this:

     success: function (result) {alert(JSON.stringify(result)); }

From the error you have posted in comments below, it appears Dictionary is not serializable. Try setting AllowGet as mentioned in other answers. If doesn't work, you will need to convert Dictionary to some other object that is serializable. Or you may follow this to serialize Dictionary to json in your controller.

How do I convert a dictionary to a JSON String in C#?

Allen King
  • 2,372
  • 4
  • 34
  • 52
  • I am getting this error: 'System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], is not supported for serialization/deserialization of a dictionary, keys must be strings or objects. – nik May 23 '17 at 15:50
1

You should add JsonBehavious if you are not getting values.

return Json(returnValue, JsonRequestBehavior.AllowGet);

Rahul
  • 427
  • 4
  • 20
1

Try this piece of code :

 return Json(returnValues, JsonRequestBehavior.AllowGet);
Abhay
  • 47
  • 1
  • 10