omatase
omatase

Reputation: 1711

JQuery AjaxComplete Method Stripping Out Custom Headers?

I am trying to follow option #3 in the solution at this SO post: A controller action which returns a partial view inserts the logon page when authorization fails

I'm running into a problem reading my custom header in the ajaxComplete method in jquery.

I have confirmed in fiddler and in chrome's debug tools that the custom header is in fact being sent back and received by the browser...

Response Headers (in Fiddler):

Server: ASP.NET Development Server/10.0.0.0
Date: Sun, 15 Jan 2012 04:00:13 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Unauthorized: 1
Cache-Control: private
Content-Length: 0
Connection: Close

Response Headers (as received by Chrome):

Cache-Control:private
Connection:Close
Content-Length:0
Date:Sun, 15 Jan 2012 04:12:13 GMT
Server:ASP.NET Development Server/10.0.0.0
Unauthorized:1
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0

Response Headers (as found from calling "getAllResponseHeaders()" on the xmlHttpRequest variable passed into ajaxComplete):

Date: Sun, 15 Jan 2012 04:42:21 GMT
X-AspNet-Version: 4.0.30319
Connection: Close
Content-Length: 65
X-AspNetMvc-Version: 3.0
Server: ASP.NET Development Server/10.0.0.0
Content-Type: application/json; charset=utf-8
Cache-Control: private

Interestingly, the function that is called upon the return of the original ajax request (as initiated by jquery) does receive the Unauthorized header.

Does anyone know what's going on here and what I can do to solve this issue?

Here's my "ajaxComplete" javascript code

$(document).ajaxComplete(function (event, request, settings) {
        alert(request.getResponseHeader('Unauthorized'));
    });

Upvotes: 2

Views: 1322

Answers (2)

omatase
omatase

Reputation: 1711

Vucetica's initial response got me thinking and I spent the last hour looking through jquery's code. I have my custom header coming back now. It looks like the trouble stemmed from an unhandled exception in my code within the success callback of the original ajax request.

Definitely something I should fix, but it seems odd that jquery would allow itself to be susceptible to that in a way that it fails silently and only affecting the custom headers. This unexpected behavior really led me in the wrong direction initially.

Anyway, thanks for your help everyone.

For completeness sake, here is my code before and after.

Before (no custom headers received in the ajaxComplete method)

$.ajax({
    type: "GET",
    url: "/Game/GetPlay/27?roundId=" + that.gameState.RoundToDisplay,
    contentType: "application/json; charset=utf-8",
    data: {},
    dataType: "json",
    success: function (play, request, settings) {
        that.play = play;
        that.startGame();
    },
    error: null,
    cache: false
});

After (working)

$.ajax({
    type: "GET",
    url: "/Game/GetPlay/27?roundId=" + that.gameState.RoundToDisplay,
    contentType: "application/json; charset=utf-8",
    data: {},
    dataType: "json",
    success: function (play, request, settings) {
        that.play = play;
        try {
            that.startGame();
        } catch(err){

        }
    },
    error: null,
    cache: false
});

Upvotes: 0

Aleksandar Vucetic
Aleksandar Vucetic

Reputation: 14953

You can take a look here. It might be helpful if you are using the same plugin (ajaxmanager) on your page. If not, check your other plugins.

Upvotes: 1

Related Questions