tags:

views:

13

answers:

1

I have an ajax post and in the controller I return nothing. In case there is a failure will the error message displayed with the follwoing code?

[AcceptVerbs(HttpVerbs.Post)]
    public void Edit(Model model)
    {
            model.Save();
    }

$.ajax({
             type: "POST",
             url: '<%=Url.Action("Edit","test") %>',
             data: JSON.stringify(data),
             contentType: "application/json; charset=utf-8",
             dataType: "html",
             success: function() {
             },
             error: function(request, status, error) {
                 alert("Error: " & request.responseText);
             }
         });
+1  A: 

I would recommend you returning an empty result:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Model model)
{
    model.Save();
    return new EmptyResult();
}

also no need to specify data type:

$.ajax({
    type: "POST",
    url: '<%=Url.Action("Edit","test") %>',
    data: JSON.stringify(data),
    contentType: "application/json; charset=utf-8",
    success: function() {
    },
    error: function(request, status, error) {
        alert("Error");
    }
});

In case the server returns a status code different than 200 the error callback will be called.

Darin Dimitrov