检索从Angular发送到php程序的数据?
这是我的函数AngularJS检索从Angular发送到php程序的数据?,php,angularjs,post,Php,Angularjs,Post,这是我的函数AngularJS 'use strict'; app .factory('userProvider', function ($rootScope , $http) { function logIn(user) { var url='http://127.0.0.1:100/suitecrm/service/rest.php'; /*$http.post(url,user) .success(function (response) {
'use strict';
app
.factory('userProvider', function ($rootScope , $http) {
function logIn(user) {
var url='http://127.0.0.1:100/suitecrm/service/rest.php';
/*$http.post(url,user)
.success(function (response) {
console.log(response);
alert(user['username']); //data is displayed normally here
alert(user['password']); //data is displayed normally here
});
*/
$http({
method: 'POST',
url: 'http://127.0.0.1:100/suitecrm/service/rest.php',
//data: 'username: user['username'] , password: user['password'] }
data: { username: user['username'] , password: user['password']}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}
return {
logIn: logIn
}
});
我想在文件rest.php中检索我的两个变量用户名和密码。
这是我获取用户名和密码的代码
$username =$_POST['username'];
$password =$_POST['password'];
但是,当我打开控制台时,会显示一个错误:rest.php中未定义用户名和密码,因此如何才能正确检索文件rest.php中的数据。我认为,除非您通过
form method=“POST”
提交,否则您需要读取/解析原始输入流
尝试:
$request = json_decode(file_get_contents('php://input'));
您可以将其放在PHP脚本的开头$request
将成为一个stdClass
对象,数据作为属性
$username = $request->username;
$password = $request->password;
或者,如果您希望将其作为assoc数组使用,请使用:
$request = json_decode(file_get_contents('php://input'), TRUE);
并访问以下数据:
$username = $request['username'];
$password = $request['password'];
这是控制台中的错误:注意:未定义索引:第3行的C:\Users\hp1\Desktop\Stage\SugarCrm Thinline\suitecrm\service\rest.php中的用户名
注意:第4行的C:\Users\hp1\Desktop\Stage\SugarCrm Thinline\suitecrm\service\rest.php中的未定义索引:密码
更新了更详细的答案