简体   繁体   English

jQuery AjaxComplete方法剥离自定义标题?

[英]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 我试图在此SO帖子中遵循解决方案中的选项#3: 当授权失败时,返回部分视图的控制器操作将插入登录页面

I'm running into a problem reading my custom header in the ajaxComplete method in jquery. 我在读取jquery的ajaxComplete方法中的自定义标头时遇到问题。

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... 我已经在提琴手和chrome的调试工具中确认,自定义标头实际上是由浏览器发送回并接收的...

Response Headers (in Fiddler): 响应标题(在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): 响应标题(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): 响应标头(从传递给ajaxComplete的xmlHttpRequest变量上调用“ getAllResponseHeaders()”时发现):

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. 有趣的是,在返回原始ajax请求(由jquery发起)时调用的函数确实会收到Unauthorized标头。

Does anyone know what's going on here and what I can do to solve this issue? 有谁知道这是怎么回事,我能做什么来解决这个问题?

Here's my "ajaxComplete" javascript code 这是我的“ ajaxComplete” JavaScript代码

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

You can take a look here . 你可以在这里看看。 It might be helpful if you are using the same plugin (ajaxmanager) on your page. 如果您在页面上使用相同的插件(ajaxmanager),则可能会有所帮助。 If not, check your other plugins. 如果没有,请检查您的其他插件。

Vucetica's initial response got me thinking and I spent the last hour looking through jquery's code. Vucetica的最初反应让我开始思考,我花了最后一个小时浏览jquery的代码。 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. 看起来麻烦源于原始ajax请求的成功回调中我的代码中未处理的异常。

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. 绝对是我应该解决的问题,但是jquery会以一种静默地失败并仅影响自定义标头的方式让自己容易受到这种影响,这似乎很奇怪。 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) 之前(在ajaxComplete方法中未收到自定义标头)

$.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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM