0

I've deployed to a QA server, and the solution is working fine for me, but on some more restrictive networks it behaves differently. The code is as follows:

function RegisterSubmit() {
    ShowSpinner();
    jQuery.post("@Url.Action("Register", "Home")", jQuery('form#register-form').serialize(),
    function (data) { }).done(function (successData) {
        // handle success
    }).fail(function (xhr, textStatus, errorThrown) {
        ShowError('error-message-register', errorThrown);
        grecaptcha.reset();
        HideSpinner();
    });
}

This successfully calls through to the controller, which writes a log to the db with the exception, and I can see this is all working correctly. Where it seems to fall over is how the error is returned to the browser. I'm using the following, which works fine generally:

[AllowAnonymous]
[HttpPost]
public ActionResult Register(RegisterViewModel model, string returnUrl)
{
    try
    {
        model.Validate();
        RecaptchaValidator.Validate(TenantId, Request);

        // happy path
    }
    catch (Exception ex)
    {
        LogException(ex);
        return ThrowJsonError(ex);
    }
}

It uses the following:

public JsonResult ThrowJsonError(string message)
{
    Response.StatusCode = (int) System.Net.HttpStatusCode.BadRequest;
    Response.StatusDescription = message;
    return Json(new {Message = message}, JsonRequestBehavior.AllowGet);
}

On just this one network, instead of showing the error message, it shows "Bad Request". Any ideas/solutions?

UPDATE As a side note, the success case on this code is working fine, so there is no issue reaching the server. The issue only comes up when we throw an error in the controller and want it to be caught in the view and displayed in the browser.

Savage
  • 2,296
  • 2
  • 30
  • 40
  • 1
    what is the logged exception?? – SilentCoder Feb 14 '17 at 09:30
  • It's a custom `ValidationException` that I throw on `model.Validate()`. It's as expected, and I would expect the `Message` part of that exception to be shown to the user. It happens usually, except in that one environment. – Savage Feb 14 '17 at 11:54
  • What do you mean by environment.. What are the configurations? – SilentCoder Feb 14 '17 at 13:28
  • Their network admin seems to have created quite a strict setup. A number of websites are banned, and often we have to request for IPs or ports to be unblocked. There was also an issue with Firefox and stapling, but the testing is not working on Chrome, so that's not the issue. In other words, I don't have a full handle on the issue, but I want to make sure there's no obvious code problem. – Savage Feb 14 '17 at 13:31
  • Update: I added a slight clarification – Savage Feb 14 '17 at 13:34

1 Answers1

0

Check this Sample code

$.ajax({ //actually approve or reject the promotion
url: @Url.Content("~/Home/Index"),
type: "POST",
data: JSON.stringify({ 
    // Those property names must match the property names of your PromotionDecision  view model
    promotionId: data.PromotionId, 
    userId: data.UserId, 
    reasonText: data.ReasonText
}),
contentType: "application/json; charset=utf-8",
success: function (data) {
    if (indicator == 'A') {
        alert('Promotion approved successfully!');
    }
    else {
        alert('Promotion rejected successfully.');
    }

    var homelink = '@Url.Action("Index","Home")';
    window.location.href = (homelink);

    returndata = data;
},
error: function (xhRequest, ErrorText, thrownError) {
    alert("Failed to process promotion correctly, please try again");
    console.log('xhRequest: ' + xhRequest + "\n");
    console.log('ErrorText: ' + ErrorText + "\n");
    console.log('thrownError: ' + thrownError + "\n");
}

});

From Continually receiving 400 (Bad Request) on jquery ajax post to MVC controller

Community
  • 1
  • 1
  • That's a slightly different way of doing things, but it doesn't identify the real issue – Savage Feb 14 '17 at 11:57
  • I use my pattern in a lot of places, so if I change the pattern that heavily, I have to change a lot of code – Savage Feb 14 '17 at 11:58
  • I couldn't find a solution, so I restructured my code to pass a boolean on the Ajax return as per the above – Savage Mar 01 '17 at 13:25