【发布时间】:2014-02-12 17:53:37
【问题描述】:
我有一个 HTML 页面,它需要使用 jQuery AJAX 函数向受 CAS 保护的 (Central Authentication Service) Web 服务发出请求。我有以下代码:
$.ajax({
type: "GET",
url: request,
dataType: "json",
complete: function(xmlHttp) {
console.log(xmlHttp);
alert(xmlHttp.status);
},
success: handleRedirects
});
request 变量可以指向 CAS 服务器 (https://cas.mydomain.com/login?service=myServiceURL) 或直接指向服务(然后应重定向回 CAS 以获取服务票证)。 Firebug 显示该请求正在发出并且它以 302 重定向返回。但是,$.ajax() 函数不处理重定向。
我写了这个函数来解决这个问题:
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 调用发送的 cookie。 (有关类似问题,请参阅this question。)
这是 AJAX 调用不处理重定向的问题,还是这里发生的事情比想象的要多?
【问题讨论】:
标签: authentication redirect jquery