0

Before updating this apiPost method using $.post successfully handles fail (a HTTPResponseMessage with a 401 Status Code is returned from the api call) (failure and always are undefined in apiPost method calling code).

  self.modelIsValid = ko.observable(true);
  self.modelErrors = ko.observableArray();

  self.apiPost = function (uri, data, success, failure, always) {
        self.isLoading(true);
        self.modelIsValid(true);
        $.post(rootPath + uri, data)
            .done(success)
            .fail(function (result) {
                if (failure == null) {
                    if (result.status != 400)
                        self.modelErrors([result.status + ':' +
                          result.statusText + ' - ' + result.responseText]);
                    else
                        self.modelErrors(JSON.parse(result.responseText));
                  self.modelIsValid(false);
                }
                else
                    failure(result);
            })
            .always(function () {
                if (always == null)
                    self.isLoading(false);
                else
                    always();
            });
    };

When stepping through the code the fail code block is hit and then dozens of lines of jquery, javascript, knockout and VM extension code is run before returning to the fail code block and the inner if and updating the modelErrors array and then setting the modelIsValid to false.

After updating the fail block is hit and the other code is then run but it never returns to the fail code block. The array never gets updated and the modelIsValid is never set to false (a ValidationError partial view is triggered by modelIsValid being false).

Original versions: jquery -1.9.1 and knockout-2.2.1.

Updated versions: jquery -3.1.1 and knockout-3.4.2.

Running on Widnows 7 Ultimate, VS2015 and the lastest Chrome. Debugging the javascript in Developer Tools.

Has something changed in jquery or knockout that breaks the fail. How would I rewrite to fix the problem?

Joe
  • 4,143
  • 8
  • 37
  • 65
  • Very interesting. Could you compare response headers you get before and after the update? Or maybe you have errors breaking further execution of the script? AFAIK there were no breaking changes in Ajax `.fail(...)` functionality of jQuery since 1.9 – Rango Aug 30 '17 at 14:01

1 Answers1

0

It turns out it had nothing to do with jQuery or Knockout. The project I'm using the updated jQuery and Knockout in is using Identity 2.0 which incorporates OWIN, which redirects to the Login page on a 401.

See Unauthorised webapi call returning login page rather than 401

I used Cuong Le's answer, posted 6/11/15. The WebAPI call now returns the 401 with no redirect.

Joe
  • 4,143
  • 8
  • 37
  • 65