调用Controller的Ajax调用给出了404错误

时间:2017-05-22 20:04:25

标签: c# jquery ajax model-view-controller

我正在使用Ajax调用来调用Controller方法但是我收到此错误: http://localhost:55942/%22GetCalculateAmortizationSchedule%22,%20%22Home%22 404(未找到)

这是我的控制器方法:

    [HttpPost]
    public ActionResult 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 Content(returnValue.ToString());
    }

这是查看代码:

 <form id="MyForm" method="post">
  ....
   <input type="submit" id="test" value="test" />
   </form>

这是Ajax代码:

$('#MyForm').submit(function (e) {

        $.ajax({
            url: '@Url.Action("GetCalculateAmortizationSchedule", "Home")',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",

            contentType: false,
            processData: false,
            success: function (result) {
                alert("success");
                console.log(result.data);
                // here in result you will get your data
            },
            error: function (result) {

            }
        });
        e.preventDefault();
    });

我认为问题是这行无法找到网址:

url: '@Url.Action("GetCalculateAmortizationSchedule", "Home")',

1 个答案:

答案 0 :(得分:1)

使用此代替URl

url:“/ home / GetCalculateAmortizationSchedule”

如果您在同一个控制器上,则不需要添加控制器名称

url: '@Url.Action("GetCalculateAmortizationSchedule")',