您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何解决关于Ajax跨域访问 session不能保存等问题

2024/6/24 4:26:51发布11次查看
根据浏览器的保护规则,跨域的时候我们创建的sessionid是不会被浏览器保存下来的,这样,当我们在进行跨域访问的时候,我们的sessionid就不会被保存下来,也就是说,每一次的请求,服务器就会以为是一个新的人,而不是同一个人,为了解决这样的办法,下面这种方法可以解决这种跨域的办法。在ajax 请求要加配置
$.ajax({ url:url, //加上 xhrfields及crossdomain xhrfields: { //允许带上凭据 withcredentials: true }, crossdomain: true, //以上 success:function(result){ alert("test"); }, error:function(){ } });

关于 withcredentialswithcredentials:
默认情况下,跨源请求不提供凭据(cookie、http认证及客户端ssl证明等)。通过将withcredentials属性设置为true,可以指定某个请求应该发送凭据。如果服务器接收带凭据的请求,会用下面的http头部来响应。
“access-control-allow-credentials: true”
如果发送的是带凭据的请求,但服务器的相应中没有包含上面这个头部,那么浏览器就不会把相应交给javascript(于是,responsetext中将是空字符串,status的值为0,而且会调用onerror()事件处理程序)。另外,服务器还可以在preflight响应中发送这个http头部,表示允许源发送带凭据的请求。
支持withcredentials属性的浏览器有firefox 3.5+、safari 4+和chrome。ie10及更早版本都不支持。
同时
注意在添加基本的允许跨域响应头之后
需要添加 access-allow-credentials:true
另外由于谷歌的安全策略
当withcredentials 为 true 时
responseheader中的 access-allow-origin 不能使用通配符 ‘*’
否则会提示
a wildcard ‘*’ cannot be us
ed in the ‘access-control-allow-origin’ header when the credentials flag is true. origin ‘http://url’ is therefore not allowed access.
其他浏览器待测试
根据浏览器的保护规则,跨域的时候我们创建的sessionid是不会被浏览器保存下来的,这样,当我们在进行跨域访问的时候,我们的sessionid就不会被保存下来,也就是说,每一次的请求,服务器就会以为是一个新的人,而不是同一个人,为了解决这样的办法,下面这种方法可以解决这种跨域的办法。在ajax 请求要加配置
$.ajax({ url:url, //加上 xhrfields及crossdomain xhrfields: { //允许带上凭据 withcredentials: true }, crossdomain: true, //以上 success:function(result){ alert("test"); }, error:function(){ } });

关于 withcredentialswithcredentials:
 默认情况下,跨源请求不提供凭据(cookie、http认证及客户端ssl证明等)。通过将withcredentials属性设置为true,可以指定某个请求应该发送凭据。如果服务器接收带凭据的请求,会用下面的http头部来响应。
“access-control-allow-credentials: true”
如果发送的是带凭据的请求,但服务器的相应中没有包含上面这个头部,那么浏览器就不会把相应交给javascript(于是,responsetext中将是空字符串,status的值为0,而且会调用onerror()事件处理程序)。另外,服务器还可以在preflight响应中发送这个http头部,表示允许源发送带凭据的请求。
支持withcredentials属性的浏览器有firefox 3.5+、safari 4+和chrome。ie10及更早版本都不支持。
 同时
注意在添加基本的允许跨域响应头之后
需要添加 access-allow-credentials:true
另外由于谷歌的安全策略
当withcredentials 为 true 时
responseheader中的 access-allow-origin 不能使用通配符 ‘*’
否则会提示
a wildcard ‘*’ cannot be used in the ‘access-control-allow-origin’ header when the credentials flag is true. origin ‘http://url’ is therefore not allowed access.
其他浏览器待测试
以上就是如何解决关于ajax跨域访问 session不能保存等问题的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product