0

I am sending data from a form via Jquery AJAX with JSON formatted data. The file processing on the server is a ASHX Handler.

Below - code snippet:

// Jquery AJAX JSON ...

$.ajax({

            type: "POST",
            url: "handler.ashx",
            data: $.toJSON(formdata),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(response) {
                $('#ajax-loader').hide();
                $('#msg').html("Ok!");
            },
            error: function(response) {
                $('#ajax-loader').hide();
                $('#msg').html("Error!");
            }
});

// handler.ashx...

public void ProcessRequest(HttpContext context)
{

    context.Response.ContentType = "text/plain";


    string json = RequestBody(context.Request);
    JavaScriptSerializer js = new JavaScriptSerializer();
    FormData formdata = js.Deserialize<FormData>(json);

    // Business Process ...


    // ...


    context.Response.Write("OK - All Right");

}

However, Jquery always performs the error instead of success callback function, even if the server everything worked. What can be happening?

Success callback not triggering. Which adjustments do you recommend?

Obs: always Status Ok 200 - Firebug Response.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • 1
    With `dataType: "json"`, jQuery will `error` if the entire response is not valid JSON. And, `context.Response.Write("OK - All Right");` will make it invalid. – Jonathan Lonowski Apr 25 '14 at 00:15
  • possible duplicate of [Ajax request return 200 OK but error event is fired instead of success](http://stackoverflow.com/questions/6186770/ajax-request-return-200-ok-but-error-event-is-fired-instead-of-success) – Jonathan Lonowski Apr 25 '14 at 00:32

0 Answers0