Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 安古拉和饼干_Angularjs - Fatal编程技术网

Angularjs 安古拉和饼干

Angularjs 安古拉和饼干,angularjs,Angularjs,我对AngularJS和cookies有些不完全理解 我有一个网址:https://ajax.foo.bar/helloworld.php 此url设置cookie,如果PHP文件找到cookie,则输出由DB生成: { DbReturnBecauseOfCookie: "foo" } 如果我加载https://ajax.foo.bar/helloworld.php 在我的浏览器中,它给我:DbReturnBecauseOfCookie:foo,因为cookie被设置为相同的浏览器和相同

我对AngularJS和cookies有些不完全理解

我有一个网址:https://ajax.foo.bar/helloworld.php 此url设置cookie,如果PHP文件找到cookie,则输出由DB生成:

{
   DbReturnBecauseOfCookie: "foo"
}
如果我加载https://ajax.foo.bar/helloworld.php 在我的浏览器中,它给我:DbReturnBecauseOfCookie:foo,因为cookie被设置为相同的浏览器和相同的url,我假设使用:

也应该输出DbReturnBecauseOfCookie:foo,但它不会输出DbReturnBecauseOfCookie:bar。这就好像服务器将Angular和我的open browser选项卡视为两个不同的实体

这怎么可能?如何让Angular使用浏览器中设置的cookie?

问题是CORS

在我的例子中,我使用的是NGINX,可以为NodeJS、Apache等找到等价的答案

location ~ .php$ { ## Execute PHP scripts
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' 'https://ajax.foo.bar';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
         }

         if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' 'https://ajax.foo.bar';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         }

         if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' 'https://ajax.foo.bar';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         }
...

您是否为每个请求调用php中的session_start?我的helloworld.php是Magento,因此:yes@KostyaShkryob找到了
location ~ .php$ { ## Execute PHP scripts
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' 'https://ajax.foo.bar';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
         }

         if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' 'https://ajax.foo.bar';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         }

         if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' 'https://ajax.foo.bar';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         }
...