Javascript 使用json、php时尝试获取非对象错误的属性
我使用AngularJS从表单获取输入,使用console.log函数将数据发送到php,并使用php解码json文件,然后检查数据库以查找与输入值匹配的信息 login.htmlJavascript 使用json、php时尝试获取非对象错误的属性,javascript,php,mysql,angularjs,json,Javascript,Php,Mysql,Angularjs,Json,我使用AngularJS从表单获取输入,使用console.log函数将数据发送到php,并使用php解码json文件,然后检查数据库以查找与输入值匹配的信息 login.html <div ng-controller="loginCtrl"> <form action="/" id="mylogin"> Username: <input type="text" id="username" ng-model="username" ><br>
<div ng-controller="loginCtrl">
<form action="/" id="mylogin">
Username: <input type="text" id="username" ng-model="username" ><br>
Password: <input type="password" id="password1" ng-model="password1">
<button type="button" ng-click="submit()">Login</button>
</form>
php/userlogin.php
<?php
$data = json_decode(file_get_contents("php://input"));
$user=$data->username;
include("include/db.php");
$select_user = mysql_query("select * from userlogin where username='".$user."'" );
$result = mysql_fetch_array($select_user);
$user_id=$view_prof['user_id'];
if($user_id != "" )
{
echo "1";
}
else
{
echo "0";
}
?>
基本上问题在于标题内容类型。
可以这样编写,或者如果您仍然无法在php中获取post变量,请告诉我:
控制器
php文件
基本上问题在于标题内容类型。
可以这样编写,或者如果您仍然无法在php中获取post变量,请告诉我:
控制器
php文件
从请求中获取数据时,您不打算使用$\u POST/$\u REQUEST吗?。还有,你用错了文件内容你能给我看一个样本吗-Friends@RaviKumar:您能试试我的代码吗?您不打算使用$\u POST/$\u REQUEST从请求中获取数据吗?。还有,你用错了文件内容你能给我看一个样本吗-Friends@RaviKumar:你能试试我的代码吗?我想在php文件@jigar7521中使用它的地方,你可以在你的php文件中使用所有类似的数据:$data=json\u decode(file\u get\u contents('php://input")); 将其替换为$data=$\u POST;让我知道,如果你想让我制作工作代码片段,我不知道你在说什么,我想在php文件@jigar7521中使用它,你可以在你的php文件中使用所有类似的数据:$data=json\u decode(file\u get\u contents('php://input")); 将其替换为$data=$\u POST;如果你想让我编写工作代码段,请告诉我我不明白你在说什么
<?php
$data = json_decode(file_get_contents("php://input"));
$user=$data->username;
include("include/db.php");
$select_user = mysql_query("select * from userlogin where username='".$user."'" );
$result = mysql_fetch_array($select_user);
$user_id=$view_prof['user_id'];
if($user_id != "" )
{
echo "1";
}
else
{
echo "0";
}
?>
app.controller('loginCtrl', function ($scope, $http) {
$scope.submit = function () {
alert($scope.username);
$http.post('php/userlogin.php',{'username' : $scope.username},{transformRequest: angular.identity, headers: {'Content-Type': undefined}).success(function(data){
console.log(data);
if (data == true) {
alert('aaa');
}
});
}
});
<?php
var_dump($_POST); // you will surely get this.
?>