簡體   English   中英

AngularJS和cookies

[英]AngularJS and cookies

我不會完全使用AngularJS和cookies。

我有一個網址: https://ajax.foo.bar/helloworld.phphttps://ajax.foo.bar/helloworld.php
此URL設置cookie,如果PHP文件找到cookie,則輸出是由DB生成的:

{
   DbReturnBecauseOfCookie: "foo"
}

如果我在瀏覽器中加載https://ajax.foo.bar/helloworld.php它會給我: DbReturnBecauseOfCookie: "foo"因為cookie被設置為相同的瀏覽器和相同的url我會假設使用:

$http.get(httpUrl)
  .then(function(result){
    console.log( result.data.DbReturnBecauseOfCookie );
....

應輸出DbReturnBecauseOfCookie: "foo" ,但它不輸出DbReturnBecauseOfCookie: "bar" 就像服務器將Angular和我的打開瀏覽器選項卡視為兩個不同的實體一樣。

怎么可能? 我怎樣才能讓Angular使用瀏覽器中設置的cookie?

問題是CORS。 http://www.w3.org/TR/cors/#access-control-allow-origin-response-header

在我的情況下,我正在使用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';
         }
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM