【问题标题】:ionic restful authentication to POST and get data from php对 POST 的 ionic restful 身份验证并从 php 获取数据
【发布时间】:2018-03-12 15:25:40
【问题描述】:

我正在开发一个 ionic 项目以创建 android 和 ios 应用程序。 我是 ionic 新手,需要帮助了解如何通过托管在我的服务器上的 restful 服务进行登录。

restful 对每个 POSTPUT 请求使用此身份验证:

username: admin
pass: 123123

所以我正在测试POST 请求。我不知道如何进行身份验证。 服务器端已设置并正在运行。并且api已经过测试,没有错误。 (使用 cocoa restclient 测试)

好的。这是我的 app.js

angular.module('tapp', ['ionic'])
.config(function($stateProvider, $urlRouterProvider){
  $stateProvider
   .state('testapp', {
     url: "/tapp",
     abstract: true,
     templateUrl: "template/menu.html",
     controller: 'MenuCtrl'
})
.state('tapp.home', {
      url: '/home',
      views: {
          'tappMenu': {
              templateUrl: 'template/home.html',
              controller: 'HomeCtrl'
          }
      }
})
.state('tapp.dash', {
      url: '/dash',
      views: {
          'tappMenu': {
              templateUrl: 'template/dash.html',
              controller: 'DashCtrl'
          }
      }
})

$urlRouterProvider.otherwise('/tapp/home');

这是我的控制器:

.controller('HomeCtrl', function($scope, $http){
    $scope.formData = [] // just testing post data
    $scope.submit = function(){
      var data = $scope.formData.push($scope.inputData);
      $http.post('http://localhost/tapp-server/posted', data);
   };
}

我的php函数(使用codeigniter rest server)

public function posted_post()
{
    $this->response(json_encode($_POST), 200);
}

这里的问题是,每当应用程序将数据发布到 php 时,我不知道如何应用基本的 RESTful 身份验证,因为每当它发布数据时,我都会收到“未授权”错误。

对于其他数据,例如使用GET 获取数据,我接收数据并完美处理。仅在此POST 部分我感到困惑。

【问题讨论】:

    标签: rest cordova ionic-framework angularjs-http


    【解决方案1】:

    检查此http://vitalflux.com/angularjs-post-data-using-ajax-spring-mvc-example/,它可能会帮助您发送帖子而不是选项。 否则,请附上您从服务器获得的响应,以便我们为您提供更多帮助

    【讨论】:

      【解决方案2】:

      是的。我知道了。 $http 是一个低级请求。对于高级别的请求,我可以使用$resource

      在 Angular 文档中找到了一些东西。

      https://docs.angularjs.org/api/ngResource/service/$resource

      也可以用这个:

      https://github.com/mgonto/restangular#table-of-contents

      【讨论】:

        【解决方案3】:

        另外,请在服务器端进行验证,因为在 localhost 上工作的 API 代码在生产服务器中的行为取决于服务器类型和身份验证模式。

        如果你遇到问题,你应该改变你的 .htaccess 如下:

        或在您当前的 .htaccess 文件中添加 SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

            <IfModule mod_rewrite.c>
        
                Options +FollowSymLinks
                RewriteEngine on
                SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
                #RewriteRule ^(.*)$ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
                #RewriteRule ^(.*)$ - [E=REMOTE_USER:%{HTTP:Authorization},L]
                #RewriteCond %{HTTP:Authorization} ^(.*)
                #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
                RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteCond $1 !^(index\.php|asset|robots\.txt)
                RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
        
            </IfModule>
        

        如果仍有问题,请更改库代码\REST_Controller.php

        在你的代码中找到这个:

            elseif ($this->input->server('HTTP_AUTHENTICATION')) 
            { 
              if (strpos(strtolower($this->input->server('HTTP_AUTHENTICATION')), 'basic') === 0) 
              {
                  list($username, $password) = explode(':', base64_decode(substr($this->input->server('HTTP_AUTHORIZATION'), 6)));
              }
            }
        

        并替换为:

            elseif ( $this->input->server('HTTP_AUTHENTICATION') || $this->input->server('REDIRECT_REMOTE_USER') || $this->input->server('REDIRECT_HTTP_AUTHORIZATION') )
                          {
        
                            $HTTP_SERVER_AUTH = ($this->input->server('HTTP_AUTHENTICATION')) ? $this->input->server('HTTP_AUTHENTICATION') : $this->input->server('REDIRECT_HTTP_AUTHORIZATION'); 
                           if(!$HTTP_SERVER_AUTH)
                           {
                             $HTTP_SERVER_AUTH = $this->input->server('REDIRECT_REMOTE_USER'); 
                           }
        
        
                            if (strpos(strtolower($HTTP_SERVER_AUTH),'basic') === 0)
                            {
                                list($username,$password) = explode(':',base64_decode(substr($HTTP_SERVER_AUTH, 6)));
                            }
        
                        }
        

        【讨论】:

          猜你喜欢
          • 2016-02-19
          • 2011-03-03
          • 1970-01-01
          • 2012-10-01
          • 2011-11-28
          • 2018-08-06
          • 2011-11-12
          • 1970-01-01
          • 2020-09-11
          相关资源
          最近更新 更多