本文介绍了CAS认证和使用jQuery AJAX重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了需要做出请求到CAS保护()网页的HTML页服务使用jQuery AJAX功能。我有以下code:

I've got an HTML page that needs to make requests to a CAS-protected (Central Authentication Service) web service using the jQuery AJAX functions. I've got the following code:

$.ajax({
    type: "GET",
    url: request,
    dataType: "json",
    complete: function(xmlHttp) {
        console.log(xmlHttp);
        alert(xmlHttp.status);
    },
    success: handleRedirects
});

要求变量可以是到CAS服务器( https://cas.mydomain.com/login?service=myServiceURL ),或直接到服务(当时应该重定向到CAS以获得服务票证)。萤火显示该请求被提出,它回来为302重定向。但是, $。阿贾克斯()函数不处理重定向。

The request variable can be either to the CAS server (https://cas.mydomain.com/login?service=myServiceURL) or directly to the service (which should then redirect back to CAS to get a service ticket). Firebug shows that the request is being made and that it comes back as a 302 redirect. However, the $.ajax() function isn't handling the redirect.

我写了这个功能来解决此问题:

I wrote this function to work around this:

var handleRedirects = function(data, textStatus) {
    console.log(data, textStatus);
    if (data.redirect) {
       console.log("Calling a redirect: " + data.redirect);
       $.get(data.redirect, handleRedirects);
    } else {
        //function that handles the actual data processing
        gotResponse(data);
    }
};

然而,即使这一点,在 handleRedirects 函数不会被调用,而 xmlHttp.status 总是返回 0 。它也不像饼干越来越与cas.mydomain.com呼叫发送。 (请参见。)

However, even with this, the handleRedirects function never gets called, and the xmlHttp.status always returns 0. It also doesn't look like the cookies are getting sent with the cas.mydomain.com call. (See this question for a similar problem.)

这是同一个AJAX调用的问题不处理重定向,还是有更多的事情在这里比满足眼睛?

Is this a problem with the AJAX calls not handling redirects, or is there more going on here than meets the eye?

推荐答案

有确实比较回事比满足眼睛。

There is indeed more going on than meets the eye.

经过一番调查,看来,如果他们不能在同一子域中取得这样做jQuery的AJAX请求失败。在这个例子中,请求被以 cas.mydomain.com 从不同的服务器进行。即使是还对 mydomain.com ,请求将失败,因为的子域名不匹配

After some investigation, it appears that jQuery AJAX requests made in this way fail if they're not made to the same subdomain. In this example, requests are being made to cas.mydomain.com from a different server. Even if it is also on mydomain.com, the request will fail because the subdomain doesn't match.

jQuery的AJAX做处理重定向正常。我做了一些测试与同一子脚本来验证。此外,饼干也通过你所期望的。请参见。

jQuery AJAX does handle redirects properly. I did some testing with scripts on the same subdomain to verify that. In addition, cookies are also passed as you would expect. See my blog post for this research.

也请记住,该协议必须是相同的。也就是说,因为 cas.mydomain.com 使用HTTPS,您从中调用它,还必须对HTTPS或请求将失败。页

Also keep in mind that the protocols must be the same. That is, since cas.mydomain.com is using HTTPS, the page from which you are calling it must also be on HTTPS or the request will fail.

这篇关于CAS认证和使用jQuery AJAX重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:23