angular $httpProvider使用配置
来源:互联网 发布:macair office办公软件 编辑:程序博客网 时间:2024/05/22 03:12
- angular.module('app', [
- 'ngAnimate',
- 'ngCookies',
- 'ngResource',
- 'ngRoute',
- 'ngSanitize',
- 'ngTouch'
- ],function ($httpProvider) {
- // 头部配置
- $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
- $httpProvider.defaults.headers.post['Accept'] = 'application/json, text/javascript, */*; q=0.01';
- $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
- /**
- * 重写angular的param方法,使angular使用jquery一样的数据序列化方式 The workhorse; converts an object to x-www-form-urlencoded serialization.
- * @param {Object} obj
- * @return {String}
- */
- var param = function (obj) {
- var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
- for (name in obj) {
- value = obj[name];
- if (value instanceof Array) {
- for (i = 0; i < value.length; ++i) {
- subValue = value[i];
- fullSubName = name + '[' + i + ']';
- innerObj = {};
- innerObj[fullSubName] = subValue;
- query += param(innerObj) + '&';
- }
- }
- else if (value instanceof Object) {
- for (subName in value) {
- subValue = value[subName];
- fullSubName = name + '[' + subName + ']';
- innerObj = {};
- innerObj[fullSubName] = subValue;
- query += param(innerObj) + '&';
- }
- }
- else if (value !== undefined && value !== null)
- query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
- }
- return query.length ? query.substr(0, query.length - 1) : query;
- };
- // Override $http service's default transformRequest
- $httpProvider.defaults.transformRequest = [function (data) {
- return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
- }];
- }).config(function ($routeProvider) {
- $routeProvider
- .when('/', {
- templateUrl: 'views/main.html',
- controller: 'MainCtrl'
- })
- .when('/about', {
- templateUrl: 'views/about.html',
- controller: 'AboutCtrl'
- })
- .otherwise({
- redirectTo: '/'
- });
- });
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
- angular $httpProvider使用配置
- 修改angular post提交json数据的格式_ $httpProvider配置
- 用angular $httpProvider做路由加载遮罩层
- 【Angular学习】Angular环境配置
- 使用 Angular
- wowza HTTPProvider 不生效
- angular http provider配置
- angular指令详细配置
- angular config路由配置
- angular-cli配置 windows
- Angular 路由配置详解
- angular环境配置
- Angular环境配置
- 【Angular】windows10下配置Angular开发环境
- Angular-使用Angular CLI快速搭建框架
- angular.copy(),angular.extend和angular.merge()方法使用总结
- angular controller的使用
- angular foreach的使用
- 【架构】REST架构风格简洁
- JAVA,从头来过,我在路上!
- 1023. 组个最小数 (20)
- 【剑指offer】复杂链表的复制
- Android 应用程序组件
- angular $httpProvider使用配置
- 情报学
- Ubuntu 配置 minicom 用来做串口调试
- 如何关闭VS2015的Brower Link功能
- spring mvc 创建及理解
- C字符串处理相关函数
- 基于Android蓝牙SPP 发送16进制数相关问题
- centos 磁盘分区扩容
- Android 5.X Color Palette的使用