1. 程式人生 > >html5 cocos2d js Access-Control-Allow-Origin

html5 cocos2d js Access-Control-Allow-Origin

hive blank 跨域問題 增加 company ive cat arch 跳轉

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

html5 cocos2d js Access-Control-Allow-Origin