html5 cocos2d js Access-Control-Allow-Origin

1.No 'Access-Control-Allow-Origin' header is present on the requested


近期在接html5的渠道,遇到了跨域的问题,使用 js 的 ajax post 或者get 方法,仅仅要目标URL略微有点不同,就会有跨域问题,浏览器会由于安全问题自己主动拦截。

这个问题事实上不是client能解决的,最好是目标URL的server代码是你能够控制的。最方便的解决方式是在你訪问的server端页面加代码。比方你如今的地址是 http://a.company.com 要訪问 http://b.company.com 。你应该在 http://b.company.com 中增加同意跨域訪问的代码。

PHP代码例如以下:

header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Credentials: true");
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');

2.跳转还是会有问题


比方你又在http://b.company.com 的server代码又跳转到 http://c.other.com , 并且 http://c.other.com 不受你控制,那么你能够使用这种小技巧。返回url给js,让js通过 location.href = "http://c.other.com" 来跳转。

http://www.waitingfy.com/archives/1737

原文地址:https://www.cnblogs.com/yangykaifa/p/7227478.html