douzi8112 2018-01-18 10:07
浏览 88
已采纳

angularjs $ http(config)来自表单的数据不起作用

I have an issue with $http in angularjs :

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


  })
    }
});

my form =>

<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>

my php code =>

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;

i can't manage to send the data via the $http(config), i have an error telling me :

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

but my index are defined, need some help to understand this one

Thanks

  • 写回答

1条回答 默认 最新

  • dongzhila3786 2018-01-18 10:11
    关注

    You have to add headers application/x-www-form-urlencoded to receive the data in GET/POST request in php.

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

    Change your $http request to this:

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

    You can also add headers for all $http request like this:

    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');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题