【问题标题】:laravel REST API set cookie to AngularJS applicationlaravel REST API 将 cookie 设置为 AngularJS 应用程序
【发布时间】:2016-04-16 03:33:17
【问题描述】:

我的服务器是一个 laravel 5 RESTFUL API,我的客户端是一个 angularJS 应用程序。当用户登录请求成功时,响应头中会有一个Set-Cookie 值。但是,cookie设置不成功,我的客户端应用程序没有设置cookie。如何为 angularJS 应用程序设置 cookie?

Access-Control-Allow-Headers:Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With,Content-Length, Accept, x-csrf-token, origin
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Allow:POST
Cache-Control:no-cache
Connection:keep-alive
Content-Length:0
Content-Type:text/html; charset=UTF-8
Date:Fri, 15 Apr 2016 18:59:01 GMT
Server:nginx
Set-Cookie:laravel_session=9c884f2b0d92d18daea36caa97955bd55e4d68e7; expires=Fri, 15-Apr-2016 20:59:01 GMT; path=/; httponly

【问题讨论】:

  • Set-Cookie:laravel_session=9c884f2b0d92d18daea36caa97955bd55e4d68e7; expires=Fri, 15-Apr-2016 20:59:01 GMT; path=/; httponly

标签: php angularjs rest laravel cookies


【解决方案1】:

只需注入ngCookies 模块:-

<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular-cookies.js'></script>

angular.module('APPinAIR', ['ngCookies'])
  .controller('APPinAIRController', ['$cookieStore', function($cookieStore) {
  // Setting a cookie
  $cookieStore.put('APPinAIR', 'You are awesome');
  // Retrieving a cookie
  var APPinAIRCookie = $cookieStore.get('APPinAIR');
  console.log('APPinAIR is ' + APPinAIRCookie);
}]);

【讨论】:

  • 请忘记不要使用//ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-cookies.js
  • @APPinAIR 很高兴它有帮助
猜你喜欢
  • 1970-01-01
  • 2020-09-07
  • 2014-08-17
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 2019-07-22
相关资源
最近更新 更多