angularjs $http(config) 来自表单的数据无效

angularjs $http(config) ith data from form not working

我对 angularjs 中的 $http 有疑问:

app.controller('ctrlProfil', function($scope, $http){

  $scope.loginProfil = "<?= $_SESSION['login']?>";
  $scope.mdpProfil = "<?= $_SESSION['mdp']?>";
  $scope.emailProfil = "<?= $_SESSION['email']?>";

  var config = {
    method: 'POST',
    url: 'modifProfil.php',
    data: $('#formProfil').serialize()
  }
  $scope.submit = function(){
  $http(config).
  then(function(response){

    console.log(response);
    console.log($('#formProfil').serialize());


  })
    }
});

我的表格=>

<form id="formProfil" ng-submit="submit()">
        <p><span>Bonjour  </span><input  type="text" name="loginProfil" value="{{loginProfil}}"/></p>
        <p>Mon mot de passe:  <input  type="text" name="mdpProfil" value="{{mdpProfil}}"/></p>
        <p> Email:  <input  type="email" name="emailProfil" value="{{emailProfil}}"/></p>
        <input class="btn btn-primary" type="submit" value="Enregistrer"/>
      </form>

我的php代码=>

try
{
  $db = new PDO('mysql:host=localhost;dbname=monprojet;charset=UTF8', 'root', 'root');
}
catch(Exception $e)
{
  die('Erreur : '.$e->getMessage());
}
$login = $_POST['loginProfil'];
$mdp = $_POST['mdpProfil'];
$email = $_POST['emailProfil'];

$rep = $db->query('SELECT id FROM utilisateur WHERE login='.$_SESSION['login']);
$reponse = $db->prepare('UPDATE utilisateur SET login= :login, mdp= :mdp, email= :email WHERE id='.$rep);
$reponse->execute(array(
  ':login' => $login,
  ':mdp' => $mdp,
  ':email' => $email
));
$json = json_encode($reponse->fetchAll());
$reponse->closeCursor();
echo $json;

我无法通过 $http(config) 发送数据,我有一个错误告诉我:

Notice: Undefined index: loginProfil in /Applications/MAMP/htdocs/izad/git/modifProfil.php on line 15

Notice: Undefined index: mdpProfil in /Applications/MAMP/htdocs/izad/git/modifProfil.php on line 17

Notice: Undefined index: emailProfil in /Applications/MAMP/htdocs/izad/git/modifProfil.php on line 19

但我的索引已定义,需要一些帮助才能理解这个

谢谢

您必须添加 headers application/x-www-form-urlencoded 才能接收 GET/POST 请求中的数据在 php.

By default, the $http service will transform the outgoing request by serializing the data as JSON

将您的 $http 请求更改为:

var config = {
            method: 'POST',
            url: 'modifProfil.php',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: $('#formProfil').serialize()
          }
          $scope.submit = function(){
          $http(config).
          then(function(response){

            console.log(response);
            console.log($('#formProfil').serialize());

      })

您还可以像这样为所有 $http 请求 添加 headers:

myapp.factory('httpRequestInterceptor', function () {
  return {
    request: function (config) {

      config.headers['Content-Type'] = 'application/x-www-form-urlencoded';

      return config;
    }
  };
});

myapp.config(function ($httpProvider) {
  $httpProvider.interceptors.push('httpRequestInterceptor');
});