AngularJS: $httpProvider.defaults.withCredentials = true;

来源:互联网 发布:淘宝二手货市场 编辑:程序博客网 时间:2024/06/03 22:05

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
原创粉丝点击