weixin_33714884 2017-05-22 23:16 采纳率: 0%
浏览 37

将Json数据返回给Ajax调用

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.

  • 写回答

3条回答 默认 最新

  • weixin_33743248 2017-05-23 02:59
    关注

    You should add JsonBehavious if you are not getting values.

    return Json(returnValue, JsonRequestBehavior.AllowGet);

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么