PiT*_*ber 7 javascript php cross-domain
我有bookmarklet.如果我打开一个随机页面(不是我的)并单击书签,我想检查用户是否已登录我的页面.
我已经使用Access-Control-Allow-Origin进行跨域AJAX请求,但看起来这里没有会话ID或cookie发送.
有没有办法做到这一点?
亚历克斯是对的!这里是完整的解决方案。(它不适用于 IE8 和 IE9!)
您需要在客户端设置withCredentials 。从 jQuery 1.5.1 开始,您可以像下面所示的那样执行此操作(源代码)。对于旧版本,请遵循白兔。
$.ajax({
url: a_cross_domain_url,
xhrFields: {
withCredentials: true
}
});
Run Code Online (Sandbox Code Playgroud)
在服务器端,您必须允许设置选项、允许凭据并允许来源。不允许使用通配符来源!但你可以从请求头中读出来源:)
// auto adapted Access Control to origin from request header.
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
if ($header == 'Origin')
header('Access-Control-Allow-Origin: ' . $value, true);
}
// send cookies from client
header('Access-Control-Allow-Credentials: true', true);
// allow all methods
header('Access-Control-Allow-Methods: GET, POST, OPTIONS', true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1395 次 |
| 最近记录: |