2015-12-09 57 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