为什么jQuery的阿贾克斯()方法不是送我的会话cookie? [英] Why is jquery's .ajax() method not sending my session cookie?

查看:117
本文介绍了为什么jQuery的阿贾克斯()方法不是送我的会话cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过 $登录。阿贾克斯()来一个网站,我想发送第二 $。阿贾克斯()要求该网站 - 但是当我检查使用萤火虫发出的头,没有会话cookie被包括在请求

After logging in via $.ajax() to a site, I am trying to send a second $.ajax() request to that site - but when I check the headers sent using FireBug, there is no session cookie being included in the request.

我是什么做错了吗?

推荐答案

AJAX调用仅发送cookie,如果你调用的URL是在同一个域中的调用脚本。

AJAX calls only send Cookies if the url you're calling is on the same domain as your calling script.

这可能是一个跨域问题。

This may be a Cross Domain Problem.

也许你试图调用一个URL从 www.domain-a.com ,而你调用脚本是在 www.domain-b.com (换句话说:你犯了一个跨域调用在这种情况下,浏览器将不会发送任何cookie以保护您的隐私)。

Maybe you tried to call a url from www.domain-a.com while your calling script was on www.domain-b.com (In other words: You made a Cross Domain Call in which case the browser won't sent any cookies to protect your privacy).

在这种情况下,你的选择是:

In this case your options are:

  • 在写一个小代理,它驻留在域B和转发您的请求域一个。您的浏览器将允许您拨打的代理,因为它是在同一台服务器调用的脚本上。
    这个代理,那么可以由你配置为接受cookie的名称和值参数,它可以发送到域一个。但对于这个工作,你需要知道cookie的名称和值在服务器上域A想认证。
  • 如果您正在获取JSON对象尝试使用 JSONP 的请求,而不是。 jQuery的支持这些。但是,你需要改变你的域服务,使其返回有效JSONP响应。
  • Write a small proxy which resides on domain-b and forwards your requests to domain-a. Your browser will allow you to call the proxy because it's on the same server as the calling script.
    This proxy then can be configured by you to accept a cookie name and value parameter which it can send to domain-a. But for this to work you need to know the cookie's name and value your server on domain-a wants for authentication.
  • If you're fetching JSON objects try to use a JSONP request instead. jQuery supports these. But you need to alter your service on domain-a so that it returns valid JSONP responds.

高兴,如果这有助于哪怕是一点点。

Glad if that helped even a little bit.

这篇关于为什么jQuery的阿贾克斯()方法不是送我的会话cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆