AJAX.BeginForm - OnFailure not receiving JSON response

354 Views Asked by At

Why isn't the OnFailure callback is getting a JSON response when deployed to an internal corporate Azure server? The OnSuccess callback always gets a response and the response is correctly converted to a JSON object, works on my laptop with IISExpress and on the Azure website, no problems.

The problem is the OnFailure callback, on my laptop it works fine, I get a responseJSON object in the XHR and I can extract and display an error message. It's when I deploy this to the Azure web server that the OnFailure function stops receiving the responseJSON, everything seems to work, OnFailure is triggered based on the HTTPResponse code, no errors in any of the logs. Just not getting the JSON object from the .Net controller???

I have a basic MVC web app written in C#. A single cshtml page with this as the Ajax.BeginForm definition

@using (Ajax.BeginForm("AjaxTest", "Home", null,
    new AjaxOptions
    {
        HttpMethod = "POST",
        OnBegin = "OnBegin",
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure"
    },
    new { id = "frmAjaxTest" }
    )
)

With these as the javascript functions...

        function OnBegin() {
            console.log("OnBegin:");
        }

        function OnSuccess(response) {
            console.log("OnSuccess:");
        }

        function OnFailure(xhr, status, error) {
            console.log("OnFailure");
        }

And a controller that will return a good or bad response depending on which button I click...

    [HttpPost]
    public ActionResult AjaxTest(AjaxTestFields model, string Success, string Failure)
    {
        dynamic jsonMessage = new { 
            returnParm1 = "Parm1",
            errorMessage= "Message"
        };
        if (!String.IsNullOrEmpty(Failure))
        { 
            HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Json(jsonMessage, JsonRequestBehavior.AllowGet);
        }
        else
        {
            HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
            return Json(jsonMessage, JsonRequestBehavior.AllowGet);
        }
    }

Any thoughts on why Azure would prevent a JSON response getting sent to the OnFailure callback???

1

There are 1 best solutions below

3
Rafael On

Have you checked the browser's error log for any additional errors that might be being captured? I've seen similar behavior produced in the browser by cross-site scripting (XSS) errors when making AJAX calls across domains.