![](/img/trans.png)
[英]Insert array of data in Database using MySQL,PHP and angular.js
[英]Using angular.js to send data to a php file
对,所以我有一个角度函数,它使用 http 方法将数据发送到 php 文件。我想处理数据的 php 代码并将其回显到页面上以确认 php 文件已处理它。 我目前收到 undefined 提醒我,我肯定应该把电子邮件的价值还给我吗?。 谢谢大家
我正在关注本教程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.
从文档:
$http
遗留承诺方法success
和error
已被弃用。 请改用标准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
从angularjs财产$http.success
承诺回调函数(这里面的第一个函数then
关闭)由response.data.email
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.