Reputation: 4691
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!!
Upvotes: 0
Views: 2055
Reputation: 1038710
It looks like some other controller action or handler is executing this request on the server. That's the reason why your controller action is never hit. And the reason why your error callback is triggered could be because this handler returns HTML or some other content type different than JSON although you explicitly instructed that you expect json using dataType: 'json'
.
So it's probably some routing issue. I invite you to look at the exact body of the response returned by the server when making this AJAX request which could provide you with more details about which portion/controller from your application might have returned it.
Upvotes: 2