1

I have the following ajax call and it always hits the error callback function every time it is called. The code in the handler is still run after the error but the success callback is never executed. What have I got wrong?

 $.ajax({
        type: "POST",
        url: "Handlers/TheHandler.ashx",
        data: {
            control1: $('[id*=control1]').val(),
            control2: $('[id*=control2]').val(),
            control3: $('[id*=control3]').val(),
            control4: $('#control4').val(),
            control5: $('[id*=control5]').val(),
            control6: $('[id*=control6]').val()
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.readyState);
            alert(textStatus);
            alert(errorThrown);
        },
        success: function (returnedValue) {
            alert("Got Here");
            alert(returnedValue);
        }
    });
General_9
  • 2,249
  • 4
  • 28
  • 46
  • jqXHR.readyState = 0 and textstatus = error. That is what I get when I alert the error parameters. – General_9 Oct 10 '12 at 03:09
  • And error thrown? What does that return? – FrankieTheKneeMan Oct 10 '12 at 03:17
  • Try `console.log(jqXHR)` to see what it's `responseText` and `status` are. – FrankieTheKneeMan Oct 10 '12 at 03:20
  • is your handler expecting the exact same data (control1, control2... ) it has to be exactly the same – Scott Selby Oct 10 '12 at 03:21
  • You may also want to use Chrome or Firefox's ability to capture requests and see exactly what the request/response pair is. I suspect a 404 or a 500. – FrankieTheKneeMan Oct 10 '12 at 03:23
  • console.log(jqXHR) returns readyState: 0, status: 0, statusText: 0 – General_9 Oct 10 '12 at 03:36
  • Hmm... the only thing I could think of is generating that data object beforehand and using `console.log()` to ensure that it's being populated correctly. Also: http://stackoverflow.com/questions/5661813/jqxhr-http-status-code-403-but-the-statuscode-is-0 this question (and its answer) imply that a status code of zero indicates non-same origin requests. Are you trying to make a request to a different domain? – FrankieTheKneeMan Oct 10 '12 at 03:49

1 Answers1

0

There might be errors while calling the handler or with the server side code. For these kind of scenarios, tool like firebug will be useful. By navigating to Net-->XHR tab, we get to see the results of the asynchronous operations. Ofcourse, we can also place a break point in Visual Studio and debug, but this allows to quickly find the server response or even whether the request is hitting the server or not, without attaching the debugger. In general, check the handler path and property names of the data variable, it must match parameters of the server side method. It assures server side code is getting called.

Sunny
  • 4,765
  • 5
  • 37
  • 72