0

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!!

Community
  • 1
  • 1
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • When the error function is hit, what's the value of its `e` parameter? Maybe it will provide you with some clue about the error message. Also you mentioned that the breakpoint you placed in your action is never hit but the server returned 200 status code. What's the content that is returned in this case? Inspect the response in the developer toolbar. I suppose that there's some other controller action that handles this request and it doesn't return JSON which is what your AJAX expects. – Darin Dimitrov Nov 29 '15 at 11:54

1 Answers1

2

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The response is the entire HTML of the page... I admit, I have removed the Controller Home from the default Routes... – MyDaftQuestions Nov 29 '15 at 12:01
  • OK, that explains why the error function is hit. So now you have to fix your routes so that the correct controller action is hit when making this request. If you put a breakpoint inside the controller action that served this page initially is it the one being hit? It looks like you have some misconfigured routes or the `url` parameter is simply wrong. – Darin Dimitrov Nov 29 '15 at 12:02