AngularJS全局修改$http中每个请求的URL
发布时间:2020-12-17 07:48:36 所属栏目:安全 来源:网络整理
导读:我们来设一个简单的例子: $scope.whatDoesTheFoxSay = function(){ $http.post("/backend/ancientMystery",{... 如何将发送请求发送到的URL全局变换为?本质上,我想为每个http请求添加一个URL. 我试过的是在应用程序启动时在$rootScope中设置一个包含url的
我们来设一个简单的例子:
$scope.whatDoesTheFoxSay = function(){ $http.post("/backend/ancientMystery",{ ... 如何将发送请求发送到的URL全局变换为?本质上,我想为每个http请求添加一个URL. 我试过的是在应用程序启动时在$rootScope中设置一个包含url的变量.但这不是我想要的代码如下: $scope.whatDoesTheFoxSay = function(){ $http.post($rootScope.backendUrl + "/backend/hidingDeepInTheWoods",{ ... 我正确的假设我应该看看$httpProvider.defaults.transformRequest?任何人都可以为我提供一些基本的示例代码?
我有另一种使用请求拦截器与$http的方法,它将在一个共同的地方处理所有的url
<!doctype html> <html ng-app="test"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script> </head> <body ng-controller="test" > <!-- tabs --> <script> var app = angular.module('test',[]); app.config(function ($httpProvider) { $httpProvider.interceptors.push(function ($q) { return { 'request': function (config) { config.url = config.url + '?id=123'; return config || $q.when(config); } } }); }); app.controller('test',function ($scope,$http) { $http.get('Response.txt').success(function (data) { alert(data) }).error(function (fail) { }); }); </script> </body> </html> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- scala – 需要使用spark中的数据库限定符访问Hive表
- java – 从一个在docker容器中运行的JVM应用程序将日志发送
- 构建基于CXF的WebService服务(1)--创建HelloWorld服务
- 基于Ubuntu14.04版本的Redis 4.0.9 版本安装
- Scala案例类私有构造函数不是私有的
- docker加速python,为什么?
- angularjs – 通过右键菜单在Web Storm中运行单个Karma Jas
- 如何检查我的Bash脚本中是否存在关联数组元素?
- bootstrap和jfinal创建省市级关联菜单
- Angular2:使用Observables进行多次同步调用的最佳方法是什