PhoneGap +传递每个HTTP请求的默认参数,不添加到每个URL

时间:2015-10-26 13:43:38

标签: angularjs cordova http phonegap-plugins

在我的PhoneGap应用程序中,我尝试使用所有HTTP请求发送默认参数。为此,我必须为每个URL添加该参数

例如:http://example.com/location/?locale=en - Locale是默认参数之一。

摘录: -

angular.module('ionic-http-auth.controllers', [])
.controller('LocationCtrl', function($ionicViewService) {
  // List of Locations
  $http.get('http://example.com/location')
    .success(function (data, status, headers, config) {
        $scope.location = data;
    })
    .error(function (data, status, headers, config) {
        console.log("Error occurred.  Status:" + status);
    });
})

.controller('CustomerCtrl', function($scope, $state, $http) {
  $scope.customers = [];
  // List of Customers
  $http.get('http://example.com/customer')
    .success(function (data, status, headers, config) {
        $scope.customers = data;
    })
    .error(function (data, status, headers, config) {
        console.log("Error occurred.  Status:" + status);
    });
})

现在要添加一个默认参数,我必须在所有URL中添加locale作为参数。他们的任何全局功能或方法是为每个URL添加默认参数。因为如果URL增加,那么我必须为每个URL添加默认的Locale。

  

http://example.com/location/?locale=en

     

http://example.com/customer/?locale=en

请提出建议..提前致谢

1 个答案:

答案 0 :(得分:0)

你应该能够使用$ http拦截器实现你的目标。这些是工厂,在应用程序发出$ http请求时使用。您可以使用请求拦截器来修改配置对象的url属性。

请参阅 https://djds4rce.wordpress.com/2013/08/13/understanding-angular-http-interceptors/了解更多信息

相关问题