Ionic framework 如何在ionic中进行HTTP基本身份验证

Ionic framework 如何在ionic中进行HTTP基本身份验证,ionic-framework,Ionic Framework,我对爱奥尼亚一无所知。在我的项目中,API都有一个基本的身份验证。API在Postman上运行正常,但不知道如何在ionic上运行。我已经为几篇文章做了研究,但在我迷失了一些步骤之后,它们需要遵循很多步骤 这是我的密码: app.controller('MainViewController', function ($scope, $http) { $http({ method:"GET", url:"my_url" }).then(function(categories)

我对爱奥尼亚一无所知。在我的项目中,API都有一个基本的身份验证。API在Postman上运行正常,但不知道如何在ionic上运行。我已经为几篇文章做了研究,但在我迷失了一些步骤之后,它们需要遵循很多步骤

这是我的密码:

app.controller('MainViewController', function ($scope, $http) {
  $http({
    method:"GET",
    url:"my_url"
  }).then(function(categories){
    console.log(categories);
  });
});
我在控制台上看到错误:

获取“我的url”401(未经授权)

以下是邮递员的设置:

然后是标题:
您知道在爱奥尼亚上执行此操作的正确方法吗?您可以在应用程序配置中设置授权标题,如下所示

app.run(['$http', function($http) {
  $http.defaults.headers.common['Authorization'] = 'Your key';
}]);
$http({
    url : "URL",
    method : 'GET',
    header : {
        Content-Type : 'application/json',    
        Authorization: 'key'
    }
 }).success(function(data){
    alert(data);
 }).error(function(error){
    alert(error);
 })
您还可以执行以下操作

app.run(['$http', function($http) {
  $http.defaults.headers.common['Authorization'] = 'Your key';
}]);
$http({
    url : "URL",
    method : 'GET',
    header : {
        Content-Type : 'application/json',    
        Authorization: 'key'
    }
 }).success(function(data){
    alert(data);
 }).error(function(error){
    alert(error);
 })

您可以在应用程序配置中设置授权标头,如下所示

app.run(['$http', function($http) {
  $http.defaults.headers.common['Authorization'] = 'Your key';
}]);
$http({
    url : "URL",
    method : 'GET',
    header : {
        Content-Type : 'application/json',    
        Authorization: 'key'
    }
 }).success(function(data){
    alert(data);
 }).error(function(error){
    alert(error);
 })
您还可以执行以下操作

app.run(['$http', function($http) {
  $http.defaults.headers.common['Authorization'] = 'Your key';
}]);
$http({
    url : "URL",
    method : 'GET',
    header : {
        Content-Type : 'application/json',    
        Authorization: 'key'
    }
 }).success(function(data){
    alert(data);
 }).error(function(error){
    alert(error);
 })
//定义字符串

var string = 'Hello World!';
//对字符串进行编码

var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
//解码字符串

var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
//定义字符串

var string = 'Hello World!';
//对字符串进行编码

var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
//解码字符串

var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"

但是我如何在每次请求api时设置基本authen呢?如果钥匙自动更换怎么办@divyesh Savaliyath显然你必须更改密钥..并且密钥不会自动更改..它在服务器端有一些逻辑这是ionic的默认代码,我如何将该代码放入此函数中<代码>app.run(函数($ionicPlatform){$ionicPlatform.ready(函数(){if(window.cordova&&window.cordova.plugins.Keyboard){cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);cordova.plugins.Keyboard.disableScroll(true);}if(window.StatusBar){StatusBar.styleDefault();}})但如何在每次请求此api时设置basic authen?如果钥匙自动更换怎么办@divyesh Savaliyath显然你必须更改密钥..并且密钥不会自动更改..它在服务器端有一些逻辑这是ionic的默认代码,我如何将该代码放入此函数中
app.run(函数($ionicPlatform){$ionicPlatform.ready(函数(){if(window.cordova&&window.cordova.plugins.Keyboard){cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);cordova.plugins.Keyboard.disableScroll(true);}if(window.StatusBar){StatusBar.styleDefault();}})