关闭

AngularJS: $httpProvider.defaults.withCredentials = true;

标签: angularxmlhttprequest
1261人阅读 评论(0) 收藏 举报
分类:

Requests_with_credentials

$httpProvider.defaults.withCredentials = true;

Whether to set the withCredentials flag on the XHR(XMLHttpRequest) object.

If you use the standard $http service to access remote APIs, it will Just Work as long as the server is configured to allow requests from your domain and you don’t need to store cookies.

But for many applications, we also need to set and store cookie information for things like logins. By default this is not allowed in most browsers and you’ll be smashing your head wondering why the cookie information isn’t being saved!

Enter: withCredentials. withCredentials is a flag set on a low-level XMLHttpRequest (AJAX) object, but in Angular we can configure our $http requests to set this flag for everything by doing:

angular.module('myApp')
.config(['$httpProvider', function($httpProvider) {
  $httpProvider.defaults.withCredentials = true;
}])
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21579次
    • 积分:1424
    • 等级:
    • 排名:千里之外
    • 原创:130篇
    • 转载:6篇
    • 译文:0篇
    • 评论:0条