最近把左边的传统模式,换成了右边通过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. 桥接设计模式(Bridge)

    Bridge??? Bridge的意思是"桥梁".就像在现实世界中,桥梁的功能是将河流的两侧连接起来一样,Bridge模式的作用也是将两样东西连接起来,它们分别是类的功能层次结构和 ...

  2. [Android]Android之四种常见布局

    一个丰富的界面总是要由很多个控件组成的,那我们如何才能让各个控件都有条不紊地 摆放在界面上,而不是乱糟糟的呢?这就需要借助布局来实现了.布局是一种可用于放置很 多控件的容器,它可以按照一定的规律调整内 ...

  3. Linux —— gcc编译文件

    编译过程 预处理: 作用: 负责展开在源文件重定义的宏 操作: g++ -E 源文件.c -o 目标文件.i 汇编: 作用: 将目标文件生成汇编代码文件 操作: g++ -S 目标文件.i -o 汇编 ...

  4. hdu5884

    #include <cstdio> #include <queue> #include <algorithm> #include <string.h> ...

  5. ASP.NET Core Linux

    环境说明 CentOS / 7.1 (64bit) (Linux操作系统) 3MySQL5.7(网站应用数据库) .NET Core SDK 2.0.0(网站应用环境) Nginx(反向代理服务器) ...

  6. HashMap与TreeMap

    package com.jckb; import java.util.Map; import java.util.Set; import java.util.TreeMap; /**TreeMap * ...

  7. c# 视频播放

    发表于: 2003-10-15 20:39:21 搞定了,嘿嘿!首先非常感谢zoujiaming在邮件中给我指了条路:用C#调用API搞定!!!使用的是mciSendString API函数主要参考了 ...

  8. spring事务的开启方式(编程式和声明式)

    1.编程式事务:编码方式实现事务管理(代码演示为JDBC事务管理) Spring实现编程式事务,依赖于2大类,分别是上篇文章提到的PlatformTransactionManager,与模版类Tran ...

  9. 兼容IE9以下的获取兄弟节点

    function fileCheck(ele){ function getNextElement(node){ //兼容IE9以下的 获取兄弟节点 var NextElementNode = node ...

  10. SharePoint Online 缺少“将站点另存为模板”

    之前文章行给出在SharePoint 2010 .SharePoint 2013 中将站点保存模板选项的文章.其实同样的问题出现在Microsoft Office 365的一部分SharePoint ...