最近在发现使用Taobao的时候的一个小细节,于是便萌发起了写这篇文章。
当我们在 www.taobao.com 中进行登录之后,然后直接切换到 www.tmall.com 域名下,发现www.tmall.com首页的最顶部马上显示成了”您好, andyfaces“,于是便对此处的实现机制进行分析。
首先,用户名应该是存储在cookie中的,于是在taobao.com的域名中用 firefox看到用户名确实是存储在 cookie, 而tmall.com中没有存储该cookie:
可以确定的是对于cookie来说肯定是不允许垮域访问的。无论是通过JS还是Server端程序来说都是如此,那么tmall.com是如何访问到taobao.com下的cookie的呢?
于是打开 tmall.com,然后使用firebug来进行调试,发现了一条这样的请求语句,
其页面的JS代码为:
- <script>
- KISSY.getScript("http://www.taobao.com/go/app/tmall/login-api.php?"+Math.random())
- </script>
看到这里之后于是也大概知道他如何处理了的,为了确认一下,于是搜索一下 KISSY.getScript 函数代码,确实采用了JS跨域的 JSONP 解决方案:
- getScript: function(url, success, charset) {
- var isCSS = RE_CSS.test(url),
- node = doc.createElement(isCSS ? 'link' : 'script'),
- config = success, error, timeout, timer;
- node.src = url;
- node.async = true;
- scriptOnload(node, function() {
- if (timer) {
- timer.cancel();
- timer = undef;
- }
- S.isFunction(success) && success.call(node);
- // remove script
- if (head && node.parentNode) {
- head.removeChild(node);
- }
- });
- head.insertBefore(node, head.firstChild);
- }
其原理是通过动态create js include 动态加载js,然后为该script节点bind onload事件或判断onreadystatechange,其具体细节可以参考以上 scriptOnload 的函数的处理。 当js加载完成之后 采用回调方式来执行 success 函数。
为了进一步确实,于是使用 Jquery的 $.getScript 来测试一把,首先在 taobao.com下进行登录成功,然后随便在本地写了一个测试页,通过以下语句:
- $.getScript('http://www.taobao.com/go/app/tmall/login-api.php?0.6783450077710154', function(){
- console.log("the taobao.com cookie object:" + userCookie + " username:" + userCookie._nk_);
- });
Firbug结果:
其实大致原理如此,通过在www.taobao.com 的server端提供一个获取当前域下所有cookie的 php的请求地址,然后该php获取到cookie之后将期并成 js 代码,也就是以上第二个截图所看到的。然后再在 tmall 采用 jsonp 的方式跨域加载该 js 代码,从而实现 cookie 的跨域访问。
http://www.iteye.com/topic/1000776
相关推荐
项目详解地址:http://www.code4app.com/blog-843201-350.html 快速集成RTMP的视频推流教程:http://www.code4app.com/blog-843201-315.html ffmpeg常用命令操作:...
/应用程序/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/
14.0真机调试包 安装路径:/应用程序/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/De
下载 sdk:http://developer.android.com/sdk/index.html 选择 linux 64 位版本 adt-bundle-linux-x86_64-20130729/sdk/platform-tools/adb 2、 安装 adb $ mkdir -p ~/bin $cp adt-bundle-linux-x86_64-20130729/sdk...
http://developer.novell.com/wiki/index.php/Libraries_for_C_(LibC) for authldap LDAP libs (2004/02/18 or later), available at: http://developer.novell.com/wiki/index.php/LDAP_Libraries_for_C you should...
百度官方pcs api文档:http://developer.baidu.com/wiki/index.php?title=docs/pcs/overview,上面有各种语言的SDK,我用的laravel5,直接导里面的php SDK还用不了,稍做了修改才能用的。 看完上面两篇文章,基本就...
IOS真机调试包 解压后放在目录:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
这是编译原理课程写的一个PL0的编译器,实现了词法分析、语法...愿意支持一下的可用积分下载,白嫖移步开源地址https://gitee.com/wlanxww/run_PL0_CPP。压缩包删除了clion的文件,开源地址理论上可以直接用(macOS)
描述文件 只能下载 iOS 13.1 的beta版 需要13.0 的话 需要自己先升级mac 系统到10.15的beta版 再去这个地址下载手机相应的固件 https://developer.apple.com/download/#ios-restore-images-iphone-new 通过ituns ...
//developer.baidu.com/map/jsdemo.htm#c1_19https://developer.baidu.com/map/jsdemo.htm#c1_19
microsoft/webview2: https://developer.microsoft.com/zh-cn/microsoft-edge/webview2/ 高德地图 Uri Api: https://lbs.amap.com/api/uri-api 百度坐标拾取系统: https://api.map.baidu.com/lbsapi/getpoint/ ...
安卓开发者网站https://developer.android.com/官网下载 国内好像不能直接访问,真是不懂这个网站为什么会被墙
- [Room](https://developer.android.com/topic/libraries/architecture/room) - [Lifecycle-aware components](https://developer.android.com/topic/libraries/architecture/lifecycle) - [ViewModels]...
下拉复选框需要的相关组件,详细可以在https://developer.snapappointments.com/bootstrap-select/里面下载
* https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial * http://mdn.github.io/webgl-examples/tutorial/sample1/ * http://mdn.github.io/webgl-examples/tutorial/sample2/ * ...
选择了阿里云视觉智能开放平台(https://vision.aliyun.com/)、百度AI开放平台...//cloud.tencent.com/)、讯飞开放平台(https://www.xfyun.cn/)、华为云(https://developer.huaweicloud.com/)等主流的国内AI平台
参考资料: http://developer.android.com/reference/android/widget/SlidingDrawer.html 现在官方建议不再使用 第二种是DrawerLayout: 这种效果是左边的侧滑栏盖在右边。 参考资料: ...
使用EclipseME开发J2ME程序(含测试源码) <br>SUN无线工具包 http://developers.sun.com/mobility/<br> 摩托罗拉 J2ME SDK http://developer.motorola.com/<br> 诺斯基亚J2ME开发者套件 2.2 ...
开发者指南: http://alibaba.github.io/dubbo-doc-static/Developer Guide-zh.htm Dubbo FAQ: http://alibaba.github.io/dubbo-doc-static/FAQ-zh.htm Dubbo社区: ...
该资源是本人根据基础地图3.2.0SDK版本和定位5.0SDK版本进行开发。 详见blog: http://blog.csdn.net/crazy1235/article/details/42614603 ...http://blog.csdn.net/crazy1235/article/details/43898451