加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读