angular $httpProvider使用配置

来源:互联网 发布:macair office办公软件 编辑:程序博客网 时间:2024/05/22 03:12
  1. angular.module('app', [  
  2.     'ngAnimate',  
  3.     'ngCookies',  
  4.     'ngResource',  
  5.     'ngRoute',  
  6.     'ngSanitize',  
  7.     'ngTouch'  
  8. ],function ($httpProvider) {  
  9.     // 头部配置  
  10.     $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';  
  11.     $httpProvider.defaults.headers.post['Accept'] = 'application/json, text/javascript, */*; q=0.01';  
  12.     $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';  
  13.   
  14.     /**  
  15.      * 重写angular的param方法,使angular使用jquery一样的数据序列化方式  The workhorse; converts an object to x-www-form-urlencoded serialization.  
  16.      * @param {Object} obj  
  17.      * @return {String}  
  18.      */  
  19.     var param = function (obj) {  
  20.         var query = '', name, value, fullSubName, subName, subValue, innerObj, i;  
  21.   
  22.         for (name in obj) {  
  23.             value = obj[name];  
  24.   
  25.             if (value instanceof Array) {  
  26.                 for (i = 0; i < value.length; ++i) {  
  27.                     subValue = value[i];  
  28.                     fullSubName = name + '[' + i + ']';  
  29.                     innerObj = {};  
  30.                     innerObj[fullSubName] = subValue;  
  31.                     query += param(innerObj) + '&';  
  32.                 }  
  33.             }  
  34.             else if (value instanceof Object) {  
  35.                 for (subName in value) {  
  36.                     subValue = value[subName];  
  37.                     fullSubName = name + '[' + subName + ']';  
  38.                     innerObj = {};  
  39.                     innerObj[fullSubName] = subValue;  
  40.                     query += param(innerObj) + '&';  
  41.                 }  
  42.             }  
  43.             else if (value !== undefined && value !== null)  
  44.                 query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';  
  45.         }  
  46.   
  47.         return query.length ? query.substr(0, query.length - 1) : query;  
  48.     };  
  49.   
  50.     // Override $http service's default transformRequest  
  51.     $httpProvider.defaults.transformRequest = [function (data) {  
  52.         return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;  
  53.     }];  
  54. }).config(function ($routeProvider) {  
  55.         $routeProvider  
  56.             .when('/', {  
  57.                 templateUrl: 'views/main.html',  
  58.                 controller: 'MainCtrl'  
  59.             })  
  60.             .when('/about', {  
  61.                 templateUrl: 'views/about.html',  
  62.                 controller: 'AboutCtrl'  
  63.             })  
  64.             .otherwise({  
  65.                 redirectTo: '/'  
  66.             });  
  67.     }); 

angular.module('myapp-http-request', []);angular.module('myapp-http-request').factory('MyRequests', function($http, $cookieStore){    return {        request: function(method, url, data, okCallback, koCallback){            $http({                method: method,                url: url,                data: data            }).success(okCallback).error(koCallback);        },        authentifiedRequest: function(method, url, data, okCallback, koCallback){            $http({                method: method,                url: url,                data: data,                headers: {'Authorization': $cookieStore.get('token')}            }).success(okCallback).error(koCallback);        }    }});

And example of usage (not tested, just for information):

angular.module('sharewebapp', ['myapp-http-request']).controller('MyController', ['MyRequests', function(MyRequests){    MyRequests.authentifiedRequest('DELETE', '/logout', '', function(){alert('logged-out');}, function(){alert('error');})}]);

0 0
原创粉丝点击