我的登录页面向不同URL的应用程序网站发出CORS(跨域资源共享)请求。我有一个简单的页面,我会ping一下以确定用户是否已经登录,如果是,则重定向他们,否则显示登录页面。我使用jQuery。
在Safari、Chrome、Firefox中都能正常工作……但在IE中不行(当然)。根据微软的说法,IE 10及更高版本应该支持带有withCredentials的CORS请求。
我正在使用jquery-2.0.3.min.js。
为什么IE11不能正常工作?有什么想法吗?
编辑:看起来它部分地工作了,因为它现在返回一个值{"id":false}。这种情况每次都发生,这意味着服务器从未获得凭据。我还发布了我的is_logged_in页面,我正在使用CodeIgniter框架。
编辑:在启用了IE安全设置下的“允许跨域数据源”后,我不再收到任何错误消息。
我收到的确切错误是:
SEC7118: XMLHttpRequest for http://mysite.net/guest/is_logged_in required Cross Origin Resource Sharing (CORS).
$.ajax({
url: 'http://mysite.net/guest/is_logged_in',
type: 'POST',
crossDomain: true,
xhrFields: {
withCredentials: true
},
dataType: 'json',
success: function(data) {
if(data.id) {
window.location.replace("http://mysite.net");
}
}
});
和
public function is_logged_in()
{
$allowed = array(
'http://mysite.net',
'http://www.mysite.net',
'http://www.mysite.com',
);
$url = $_SERVER['HTTP_REFERER'];
$url = substr($url, 0, strpos($url, '/', 8));
if(isset($_SERVER['HTTP_ORIGIN']))
{
if(in_array($_SERVER['HTTP_ORIGIN'], $allowed))
{
$this->output->set_header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
}
}
else
{
if(in_array($url, $allowed))
{
$this->output->set_header('Access-Control-Allow-Origin: ' . $url);
}
}
$this->output->set_header('Access-Control-Allow-Headers: X-Requested-With');
$this->output->set_header('Access-Control-Allow-Credentials: true');
$this->output->set_header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
//TODO: Try to detect if this is an ajax request, and disallow it if not.
$data = new stdClass();
$this->load->library("ion_auth");
if($this->ion_auth->logged_in())
{
$data->name = $this->ion_auth->user()->row()->first_name;
$data->id = $this->ion_auth->get_user_id();
} else {
$data->id = false;
}
$this->output->set_output(json_encode($data));
}
提前致谢
location.replace
的问题?首先尝试仅使用window.alert(data)
检查代码是否进入成功块。 - eithed