代码之家  ›  专栏  ›  技术社区  ›  CHARAFI Saad

检索对象(PHP和ANGULARJS)

  •  4
  • CHARAFI Saad  · 技术社区  · 8 年前

    我想检索一个从Angular函数发送到PHP程序的对象。 以下是我的观点:

    <ion-view>
    <ion-content padding="true">
    <div class="card">
      <div class="item item-divider text-center">
        Authentification
      </div>
      <div class="item item-text-wrap text-center">
        <form>
          <div class="list">
            <label class="item item-input">
              <input type="text" placeholder="Nom d'utilisateur" ng-model="user.username">
            </label>
    
            <label class="item item-input">
              <input type="password" placeholder="mot de passe" ng-model="user.password">
            </label>
    
    
          </div>
        </form>
    
      </div>
      <div class="item item-divider text-center">
        <a  href="" class="button button-positive button-small" ng-click="logIn(user)">
          <i class="ionicons ion-android-share"></i>
          Identifiez moi, vite !
        </a>
        <a  href="" class="button button-energized button-small">
          <i class="ionicons ion-android-mail"></i>
          Mot de passe perdu !
        </a>
      </div>
    </div>
    <button class="button button-large button-full button-positive">
      je n'ai pas de compte
    </button>
    

    这里是控制器:

    'use strict';
    
    app
    .controller('homepageIndex',function ($scope) {
    })
    .controller('homepageLogin',function ($scope , userProvider) {
      $scope.user={};
    
      $scope.logIn = function (user) {
        console.log($scope.logIn);
        console.log($scope.user);
        userProvider.logIn(user);
      }
    
    
     })
     ;
    

    'use strict';
    
    app.factory('userProvider', function ($rootScope , $http) {
    
      function logIn(user) {
        var url='http://127.0.0.1:100/suitecrm/service/rest.php';
    
        $http.post(url,user)
          .success(function (response) {
          console.log(response);
            console.log(url);
    
          });
    
      }
    
      return {
        logIn: logIn
      }
    });
    

    在我的文件中 rest.php 我想检索包含用户名和密码的对象用户:

    $username =$_POST['username'];
    $password =$_POST['password'];
    

    这个方法行不通,我想知道如何在rest.php中检索用户名和密码 谢谢你的帮助。

    2 回复  |  直到 8 年前
        1
  •  4
  •   Mohit Tanwani    8 年前

    尝试以下代码。

    var req = {
     method: 'POST',
     url: 'http://127.0.0.1:100/suitecrm/service/rest.php',
     data: { username: 'username', password:  'password' }
    }
    
    $http(req).then(function(){
       //Success
    }, function(){
    
    });
    
        2
  •  1
  •   CHARAFI Saad    8 年前

    @莫希特·坦瓦尼

    $request = json_decode(file_get_contents('php://input'));
    

    您可以将其放在PHP脚本的开头$然后,请求将是一个stdClass对象,其数据作为属性。

    $username = $request->username;
    $password = $request->password;
    

    或者,如果希望将其用作关联数组,请使用:

    $request = json_decode(file_get_contents('php://input'), TRUE);
    

    并访问以下数据:

    $username = $request['username'];
    $password = $request['password'];