nik nik - 1 year ago 44
jQuery Question

Ajax call to call a Controller give 404 error

I am using Ajax call to call a Controller method but I am getting this error:
http://localhost:55942/%22GetCalculateAmortizationSchedule%22,%20%22Home%22 404 (Not Found)

This is my Controller method:

[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());
}


This is the View code:

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


and this is the Ajax code:

$('#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();
});


I think problem is this line that cannot find the url:

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

Answer Source

Use this instead of URl

url: "/home/GetCalculateAmortizationSchedule"

If you on same controller then do not need to add controller name

url: '@Url.Action("GetCalculateAmortizationSchedule")',
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download