Lempkin
Lempkin

Reputation: 1588

What to send to call the $.post.fail()

I call a server side method with $.post() which calls a WS. If this WS sends me back an error, I would like the $.post() to fail, and then go in the

.fail(function(error) {
    // display error message
});

My server method:

public JsonResult myMethod(string reference, long quantity, ControllerContext context)
{
    wsResponse = callWS(reference, quantity);

    if (GenericUtils.isError(wsResponse))
    {
        // ????
    }
    else
    {
        return Json(response);
    }
}

Upvotes: 2

Views: 128

Answers (2)

ManojAnavatti
ManojAnavatti

Reputation: 644

public ActionResult myMethod(string reference, long quantity, ControllerContext context)
{
    wsResponse = callWS(reference, quantity);

    if (GenericUtils.isError(wsResponse))
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Something failed");
    }
    else
    {
        return Json(response);
    }
}

Upvotes: 2

T'lash
T'lash

Reputation: 573

You have to send the response from the server with an HTTP error code like 400 Bad Request, 401 Unauthorized, ... depending of the error.

jQuery only considers 200 to 299 and 304 as success, so anything else will be processed by .fail().

Upvotes: 2

Related Questions