最近把左边的传统模式,换成了右边通过js直接调api拿数据并渲染,于是变出现了ajax的跨域问题:
XMLHttpRequest cannot load http://api.abc.com/?s=user/account_log&v=1.0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://m.abc.com' is therefore not allowed access.
api项目都为post请求且返回结果为json,为了不改动api,于是没用jsonp,而是采用header,修改api.abc.com的nginx配置:

add_header Access-Control-Allow-Origin http://m.abc.com;

请求成功之后发现cookie无法共享,在ajax里带上参数:

 crossDomain: true,
xhrFields:{
withCredentials:true
},

出现错误:
XMLHttpRequest cannot load http://api.abc.com/?s=user/account_log&v=1.0. The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://m.abc.com' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
再次修改api.abc.com的nginx配置:

add_header Access-Control-Allow-Credentials true;

至此正常访问。

-------------------------2017.10.13 更新-----------------------------

如果Access-Control-Allow-Origin配置的是通配的 * ,这里还会报另一个错误

Failed to load http://api.abc.com/?s=user/account_log&v=1.0: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://m.abc.com' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

-------------------------2017.05.23 更新-----------------------------

为了配合新增m的三级域名,调整api.abc.com的nginx配置:

 server  {
listen ;
listen ;
server_name api.abc.com;
index index.php;
root /datas/htdocs/abc_api; ssl on;
ssl_certificate /etc/ssl/ssl.crt;
ssl_certificate_key /etc/ssl/ssl.key; location ~ .*\.php?$ {
set_by_lua $http_referer_test '
if ngx.var.http_referer ~= nil then
tt = string.match(ngx.var.http_referer, "//%w+%.?m%.abc%.com");
end
if tt == nil or tt == "" then
tt = "//m.abc.com";
end
return tt;
'; proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:9504;
add_header Access-Control-Allow-Origin $scheme:$http_referer_test;
add_header Access-Control-Allow-Credentials true;
} access_log /datas/log/www/access.abc_api.log main;
error_log /datas/log/www/error.abc_api.log;
}

记一次header跨域与cookie共享的更多相关文章

  1. CP="CAO PSA OUR" 用P3P header解决iframe跨域访问cookie

    1.IE浏览器iframe跨域丢失Session问题 在开发中,我们经常会遇到使用Frame来工作,而且有时是为了跟其他网站集成,应用到多域的情况下,而Iframe是不能保存Session的因此,网上 ...

  2. PHP 通过设置P3P头来实现跨域访问COOKIE

    CentOS的系统(Linux 内核) 编辑HOST vi /etc/hosts 加入127.0.0.1 www.a.com127.0.0.1 www.b.com 首先:创建 a_setcookie. ...

  3. PHP 跨域写cookie

    实际工作中,类似这样的要求很多,比如说,我们有两个域名,我们想实现在一个域名登录后,能自动完成另一个域名的登录,也就是PASSPORT的功能. 我只写一个大概,为了测试的方便,先编辑hosts文件,加 ...

  4. ajax 跨域携带COOKIE

    这个问题属于Ajax跨域携带Cookie的问题,找了一篇博文的解决方案. 原生ajax请求方式: var xhr = new XMLHttpRequest(); xhr.open("POST ...

  5. SSO单点登录、跨域重定向、跨域设置Cookie、京东单点登录实例分析

    最近在研究SSO单点登录技术,其中有一种就是通过js的跨域设置cookie来达到单点登录目的的,下面就已京东商城为例来解释下跨域设置cookie的过程 涉及的关键知识点: 1.jquery ajax跨 ...

  6. 通过设置P3P头来实现跨域访问COOKIE

    通过设置P3P头来实现跨域访问COOKIE 实际工作中,类似这样的要求很多,比如说,我们有两个域名,我们想实现在一个域名登录后,能自动完成另一个域名的登录,也就是PASSPORT的功能. 我只写一个大 ...

  7. node下的跨域传递cookie

    研究背景: 最近有一位朋友找工作,需要面试,涉及到面试就涉及面试题,于是我想起来鄙人之前面试被问到的一个跨域传递cookie的问题.搜索了相关资料,但自己不敲一下肯定是不足以让人信服的. 我用node ...

  8. 京东商城跨域设置Cookie实现SSO单点登陆过程

    可以先看下这边文章:http://blog.chinaunix.net/uid-25508399-id-3431705.html   1.点击首页的登陆按钮跳转到京东的登陆中心https://pass ...

  9. 在IE浏览器中iframe跨域访问cookie/session丢失的解决办法

    单点登录需要在需要进入的子系统B中添加一个类,用于接收A系统传过来的参数: @Action(value = "outerLogin", results = { @Result(na ...

随机推荐

  1. [LOJ 2070] 「SDOI2016」平凡的骰子

    [LOJ 2070] 「SDOI2016」平凡的骰子 [题目链接] 链接 [题解] 原题求的是球面面积 可以理解为首先求多面体重心,然后算球面多边形的面积 求重心需要将多面体进行四面体剖分,从而计算出 ...

  2. Uva12210-A Match Making Problem

    对于每个数字二分找到大于等于它的数,再暴力找到第一个小于它的数 #include<bits/stdc++.h> #define inf 0x3f3f3f3f ; using namespa ...

  3. Windows下用cpu模式跑通目标检测py-faster-rcnn 的demo.py

    关键字:Windows.cpu模式.Python.faster-rcnn.demo.py 声明:原文发表在博客园,未经允许不得转载!!!本篇blog过程已经多名读者实践验证,有人反馈报错TypeErr ...

  4. GitHub上优秀Android 开源项目

    GitHub在中国的火爆程度无需多言,越来越多的开源项目迁移到GitHub平台上.更何况,基于不要重复造轮子的原则,了解当下比较流行的Android与iOS开源项目很是必要.利用这些项目,有时能够让你 ...

  5. Myeclipse发布第一个jsp页面及web project部署到tomcat上的几种方法

    菜鸟日记: 1:new web project: 2:fix the visiting  path of the tomcat,打开在安装目录下conf目录中的server.xml,在</Hos ...

  6. wordpress安装后首页无法进入 The file 'wp-config.php' already exists

    问题是缓存导致,具体还没研究是怎么产生的缓存.chrome浏览器解决方法: 1. 网址后面加参数进入网站 2. 打开控制台-network 3. 刷新页面 4. 控制台-network,右键请求的文件 ...

  7. ios 利用runtime任性跳转

    在开发项目中,会有这样变态的需求: 推送:根据服务端推送过来的数据规则,跳转到对应的控制器 feeds列表:不同类似的cell,可能跳转不同的控制器(嘘!产品经理是这样要求:我也不确定会跳转哪个界面哦 ...

  8. git如何强制用远程分支更新本地

    git本地即使有修改如何强制更新: 本地有修改和提交,如何强制用远程的库更新本地.我尝试过用git pull -f,总是提示 You have not concluded your merge. (M ...

  9. 【虚拟机-网络IP】使用 Powershell 设置 VNET 中的静态 IP

    本文包含以下内容 对已有虚机设置静态 Internal IP 取消对对已有虚机设置的静态 Internal IP 创建静态 Internal IP的虚机 使用中的注意点 请注意:以下操作需要下载最新版 ...

  10. SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇)

    SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇) 最近真的没有什么精力写文章,天天加班,为了完成这个系列,硬着头皮上了 再看这篇文章之前请大家先看我之前写的第一篇和第二篇 第一篇: ...