I'm trying to use ajax to post back to my controller. The problem I have the breakpoint I put in my controller is never hit.
var result = {
Name: document.getElementById("ProjectItem_Name").value,
Description: document.getElementById("ProjectItem_Description").value,
DependancyTaskName: dependancyTask
};
ajaxCall.postNow("NewTask", JSON.stringify(result), success, failed, failed);
As you can see, I push everything to a function called postNow, which is
var ajaxCall = new function () {
this.postNow = function (url, data, successDelegate, failDelegate, errorDelegate) {
$.ajax({
type: "POST",
url: url,
contentType: "application/json;",
data: data,
dataType: "json",
success: function (response) {
successDelegate(response);
},
failure: function (e) {
failDelegate(e);
},
error: function (e) {
errorDelegate(e);
}
});
};
}
This is my HomeController
[HttpPost]
public JsonResult NewTask(string Name, string Description, string DependancyTaskName)
{
return Json("true", JsonRequestBehavior.AllowGet);
//also tried return Json(new { success=true }, JsonRequestBehavior.AllowGet);
}
I will also point out that if I step through the javascript in Chrome, when the code reaches the $ajax call, it jumps straight to the error function straight away. I put a break point on my controller, and noticed it is never hit.
Ajax request returns 200 OK, but an error event is fired instead of success shows the issue usualy with the incorrect return type but as my controller isn't hit I suspect it's not getting a return type at all.
I tried to change the URL and think this is where the issue is. If I pass the URL "NewTask" it shows me statusCode 200 but the error is hit. However, if I then rename the url to ThisDoesNotExist, then the same issue continues. Oddly, if I change it to Home/NewTask then I get a 404 on the post!
Then, if I comment out dataType: "json",
from the ajax call, I get the success function but still my Controller is never hit!!