向 POST 的离子 restful 身份验证并从 php 获取数据

ionic restful authentication to POST and get data from php

我正在开发一个离子项目来创建 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 服务器)

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

这里的问题是,我不知道如何应用基本的 restful 身份验证,只要应用程序将 post 数据发送到 php 因为每当它 post数据,我收到错误 "not authorized"。

对于其他数据,例如使用GET获取数据,我接收数据并完美处理。只有这 POST 部分我很困惑。

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

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

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

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

也可以用这个:

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

Also, please verify on server side since the API code working on localhost behaves different in production server depending on server type and auth mode.

If you facing problem, you should change your .htaccess as below:

OR add SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION= in your current .htaccess file:

    <IfModule mod_rewrite.c>

        Options +FollowSymLinks
        RewriteEngine on
        SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=
        #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/ [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond  !^(index\.php|asset|robots\.txt)
        RewriteRule ^(.*)$ ./index.php?/ [L,QSA]

    </IfModule>

If you still has problem, change code of libraries\REST_Controller.php

Find this in your code:

    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)));
      }
    }

and replace with:

    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)));
                    }

                }