使在codeigniter CORS(由@chriskacerguis restserver) [英] enabling cors in codeigniter ( restserver by @chriskacerguis )

查看:217
本文介绍了使在codeigniter CORS(由@chriskacerguis restserver)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在http.get控制器agularJs要求正常工作。
当API被移动到服务器,问题相比发生。

http.get request in agularJs controller works fine when my client app and api are in localhost. when api is moved to server., issue arised.

使用angularJs客户端

client side using angularJs

$http.get('http://domain.com/api/spots/2/0').success(function(datas){
console.log(datas);
});

日志给:
跨域请求阻止:同源策略不允许在 http://domain.com读取远程资源/ API /点/ 2/0 。这可以由资源移动到相同域或启用CORS是固定的。

log gives: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://domain.com/api/spots/2/0. This can be fixed by moving the resource to the same domain or enabling CORS.

我有这两条线添加到我的控制器构造

i have added these two lines to my controller construct

header("Access-Control-Allow-Origin: *");

header("Access-Control-Allow-Methods: GET");

还是同样的错误。

still same error.

推荐答案

尝试添加选项来允许的方法。

Try adding OPTIONS to the allowed methods.

header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");

和立即返回时,请求方法选项,一旦你已经设置的标头。

and return immediately when the request is method 'OPTIONS' once you have set the headers.

if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
    die();
}

又见<一个href=\"http://stackoverflow.com/questions/15602099/http-options-error-in-phil-sturgeons-$c$cigniter-restserver-and-backbone-js?rq=1\">this回答。

角发送 W3C CORS遵循规范的preflight申请了将检查允许从右方法在实际尝试它。

Angular sends a W3C CORS spec compliant preflight request that will check for the right allowed methods before actually attempting it.

就个人而言,我觉得 Mozilla开发者网络CORS页面有点更容易阅读就此事,以帮助了解CORS的流量。

Personally, I find the Mozilla Developer Network CORS page a bit easier to read on the matter to help understand the flow of CORS.

这篇关于使在codeigniter CORS(由@chriskacerguis restserver)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆