0

I have an MVC webapp that validates form input from an Ajax post in the controller like so:

if (string.IsNullOrWhiteSpace(txtNewTAmount) || !decimal.TryParse(txtNewTAmount, out _tmpD))
            return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Please Enter a Valid Transaction Amount.");

When this check fails, I have a toastr pop-up that displays the error message. In the above example, it displays "Please Enter a Valid Transaction Amount.". Here is the relevant JS code for this operation:

Ajax Post:

function submitNewTransaction() {
        var frm = $('#insertTrans');
        var postData = frm.serialize()

        $.ajax({
            type: "POST",
            url: '@Url.Action("Action","Controller")',
            data: postData,
            success: function (data) {
                OnSuccessNewTransaction(data);
            },
            error: function (data) {
                OnFailureNewTransaction(data);
            }
        });
    }

OnFailure:

function OnFailureNewTransaction(response) {

            toastr.error('Error Saving New Transaction: <br /> <strong>' + response.statusText + '</strong>', '', { progressBar: true, timeOut: 5000 });
        }

This works fine over regular HTTP, but when using HTTPS all the custom error messages in response.statusText are overwritten with just "error". I'm not positive, but I suspect IIS might be the culprit here. Is there any way I can prevent this from happening? Or should I just move all of my validation client side? Thanks in advance for any help with this matter!

JugPad
  • 11
  • 3
  • Similar issue: https://stackoverflow.com/questions/8900001/iis-overwriting-http-response-text-when-http-response-status-set-as-400/17026545 – DirectionUnkown Apr 07 '21 at 13:25

4 Answers4

1

I got the same error. Fixed by settings of IIS Go to IIS->Your site->Bindings->Select https-> Enable checkbox "http/2 deactivate"

0

Think this is an issue with IIS trying to use custom error response rather sending the error message that the controller is generating.

<system.webServer>
...
<httpErrors existingResponse="PassThrough"></httpErrors>
...

OR

Response.TrySkipIisCustomErrors = true;
0

Well I didn't find an easy config fix, but I found a reasonable workaround in case anybody else stumbles upon this problem.

I just changed the returned HttpStatusCodeResult into Json objects like so:

if (string.IsNullOrWhiteSpace(txtNewTAmount) || !decimal.TryParse(txtNewTAmount, out _tmpD))
        return Json(new { status = "Error", errMsg = "Please Enter a Valid Transaction Amount." });

Then do something like this in the JS:

$.ajax({                 
     type: "POST",
     url: '@Url.Action("Action", "Controller")',
     dataType: "html",
     data: { 'foo': bar },
     success: function (data) {    
        try{
            var JsonResp = JSON.parse(data);
            if (JsonResp.status == "Error") {
                toastr.error('<strong>' + JsonResp.errMsg + '</strong>', '', { progressBar: true, timeOut: 3000 });
                return false;
            }
        }
        catch(e){
            //not JSON
        }

        $("#myDiv").html(data);
     },
     error: function (data) {                     
         toastr.error('<strong>' + data.statusText + '</strong>', '', { progressBar: true, timeOut: 3000 });
     }
 })

'success' gets called instead of 'error' now so I just try to parse Json out of the response in 'success'. If that fails, then the PartialView gets updated as in a normal 'success'. Otherwise, the custom error message will now survive all the way to the client over HTTPS.

JugPad
  • 11
  • 3
0

I changed my ajax signature to include the additional parameters:

function (data, status, error) {}

I now get the the statusDescription from my controller as:

try
{
     throw new Exception("Invalid key");
}
catch (Exception ex)
{
     return new HttpStatusCodeResult(401, ex.Message);
}

and can retrieve it via the error parameter (error = statusDescription):

error: function (data, status, error) {
     $("#error").html(error);
},