2016-04-26 22 views
2

正確,所以我有一個角色功能,使用http方法將數據發送到php文件。php代碼我想處理數據並將其回顯到頁面上確認php文件已經處理了它。我目前正在收到未定義的警告,我當然應該將電子郵件的重要性回饋給我。感謝所有使用angular.js發送數據到一個php文件

我下面這個教程https://codeforgeek.com/2014/07/angular-post-request-php/

var request = $http({ 
method: "post", 
url: "functions.php", 
data: { 
    email: $scope.email, 
    pass: $scope.password 
}, 
headers: { 'Content-Type': 'application/x-www-form-urlencoded' } 
}); 

//.then occurs once post request has happened 
//success callback 
request.success(function (response) { 
    alert(response.data); 
}, 

//error callback 
function errorCallback(response) { 
    // $scope.message = "Sorry, something went wrong"; 
    alert('error'); 
}); 

我的PHP代碼...

//receive data from AJAX request and decode 
$postdata = file_get_contents("php://input"); 
$request = json_decode($postdata); 

@$email = $request->email; 
@$pass = $request->pass; 

echo $email; //this will go back under "data" of angular call. 

回答

1

documentation

$http遺留承諾方法successerror已被棄用。改爲使用標準then方法。如果$httpProvider.useLegacyPromiseExtensions設置爲false那麼這些方法將拋出$http/legacy錯誤。

您的代碼應該是這樣的:

request.then(
    function(response) { 
     var data = response.data; 
     console.log(data); 
    }, 
    function(response) { 
     alert('error'); 
    } 
); 

現在,你需要編碼從一個JSON格式服務器的響應,所以更換echo $email;有:

echo json_encode(array(
    'email' => $email 
)); 

,你可以請訪問email屬性,該屬性來自於承諾回調函數(這是then封閉內的第一個函數)response.data.email

+0

感謝您的幫助:) – Ryan

相關問題