2015-12-09 59 views
0

我是一个新的Angularjs用户。我面临一个问题,当我提交注册表单时,我已经应用了使用AngularJs进行验证。同时,如果所有的输入字段都是有效的,那么我发送一个$ http Ajax调用来检查电子邮件地址,已经存在或不存在。问题是我的php文件没有收到电子邮件数据。使用角度js的Ajax调用

$http({ 
     method : 'POST', 
     async: false, 
     url: 'http://localhost/angular/signup/emailcheck.php', 
     data: { email: $scope.user.email }, // pass in data as strings 
     headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) 
     }) 
     .success(function(data) 
     { 
      $scope.info = data; 
      if($scope.userForm.$valid && $scope.info === '0') { 
       alert('our form is amazing' + $scope.info); 
      } 
      else{ 
        alert('Already exist'); 
       } 
     }).error(function(response,status) 
     { 
      console.log('ERROR HERE'+ status); 
     }); 

我的PHP文件中的代码:

$email = $_POST['email']; 
$sql = "SELECT * FROM user where username = '".$email."'"; 
$result = mysql_query($sql); 
//fetch tha data from the database 
while ($row = mysql_fetch_array($result)) { 
.... 
.... 
.... 
.... 
.... 
} 

我已经检查,发现PHP文件没有收到电子邮件价值可言。

+0

检查控制台日志如果你得到一些错误? –

+0

在浏览器的控制台中是否有错误? – Achu

+0

不,在控制台没有任何错误,我检查过它。 –

回答

0

我刚才发现,在PHP文件,

$ _POST或$ _GET将无法正常工作,接收数据。 使用以下:

$data = file_get_contents("php://input"); 
$objData = json_decode($data); 
$email = $objData->email; 

对我来说,它的工作原理。

0

只是一个猜测:你的网址指向本地主机,但没有端口号,这是不寻常的,也许你忘了它?

+1

什么是不寻常的呢?它将默认为80端口,他(Apache?)服务器将处理它并运行他的PHP脚本。 – Mawg

1
$http({ 
    method : 'POST', 
    async: false, 
    url: 'http://localhost/angular/signup/emailcheck.php', 
    data : $.param($scope.user), // this will definitely wor 
    headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) 
    }) 
    .success(function(data) 
    { 
     $scope.info = data; 
     if($scope.userForm.$valid && $scope.info === '0') { 
      alert('our form is amazing' + $scope.info); 
     } 
     else{ 
       alert('Already exist'); 
      } 
    }).error(function(response,status) 
    { 
     console.log('ERROR HERE'+ status); 
    }); 
1

尝试删除从URL http://localhost,然后看到它可能是CORS。

+0

是的,它听起来像是CORS。 OP应该得到一个浏览器插件来允许它(并且只能使用该浏览器进行开发) – Mawg