Javascript 使用angular.js将数据发送到php文件
好的,我有一个angular函数,它使用http方法将数据发送到php文件。我想要处理数据的php代码,并将其回显到页面上,以确认php文件已经处理了它。我现在收到了未定义的警报,我当然应该把电子邮件的价值还给我?。谢谢大家 我正在学习这个教程 我的php代码Javascript 使用angular.js将数据发送到php文件,javascript,php,angularjs,http,Javascript,Php,Angularjs,Http,好的,我有一个angular函数,它使用http方法将数据发送到php文件。我想要处理数据的php代码,并将其回显到页面上,以确认php文件已经处理了它。我现在收到了未定义的警报,我当然应该把电子邮件的价值还给我?。谢谢大家 我正在学习这个教程 我的php代码 //receive data from AJAX request and decode $postdata = file_get_contents("php://input"); $request = json_decode($postd
//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
已被弃用。改用标准的然后方法。如果$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
) );
您还可以通过response.data.email
从angularjs$http.success
promise回调函数(这是中的第一个函数,然后是闭包)访问email
属性,谢谢您的帮助:)
echo json_encode( array(
'email' => $email
) );