2
我正在通过形式细节从离子到php文件throuugh控制器。如果我通过邮递员直接输入表格数据,它将被存储。但是,当我尝试通过填写表单数据,我得到这个错误:json返回[对象对象]提交点击
每当我点击提交按钮,我得到这个错误提示框.- [目标对象]
I have also added the header in php but still am getting the error and alert box shows null. The data to be returned shows null all the time. I am not able to figure out the problem. Please help!!!
这里是我的在那里我加入了访问控制PHP代码允许头
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
require_once 'db_functions.php';
$db = new db_functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['contact']) && isset($_POST['gender']) && isset($_POST['user_name']) && isset($_POST['password']))
{
if(is_numeric($_POST['name']) && is_numeric($_POST['gender']))
{
$response["status"] = false;
$response["error_msg"] = "Name and gender cannot have numeric values";
echo json_encode($response);
}
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
$response["status"] = false;
$response["error_msg"] = "Enter a valid email id";
echo json_encode($response);
}
else
{
// receiving the GET params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$contact = $_POST['contact'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
// check if user is already existed with the same email
if ($db->isUserExisted($user_name)) {
// user already existed
$response["error"] = false;
$response["error_msg"] = "User already existed with " . $user_name;
echo json_encode($response);
}
else if ($db->isUserEmailExisted($email)) {
// user already existed
$response["error"] = false;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
}
else if ($db->isUserContactExisted($contact)) {
// user already existed
$response["error"] = false;
$response["error_msg"] = "User already existed with " . $contact;
echo json_encode($response);
}
else {
// create a new user
$user = $db->storeUser($name, $email,$contact,$gender,$user_name,$password);
if ($user) {
// user stored successfully
$response["error"] = true;
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["contact"] = $user["contact"];
$response["user"]["gender"] = $user["gender"];
$response["user"]["user_name"] = $user["user_name"];
$response["user"]["encrypted_password"] = $user["encrypted_password"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($user);
} else {
// user failed to store
$response["error"] = false;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
}
} else {
$response["error"] = false;
$response["error_msg"] = "Required parameters are missing!";
echo json_encode($response);
}
?>
这是我的控制器代码
angular.module('app.controllers', [])
.controller('loginCtrl', function($scope) {
})
.controller('selectYourRoleCtrl', function($scope) {
})
.controller('userDetailsCtrl', function($scope,$http) {
$scope.users = {};
$scope.users.gender = "Male";
$scope.regUser = function(){
$http.post("http://localhost/drmedic/register_user.php",$scope.users)
.success(function(data){
alert(data);
})
.error(function(data){
alert(data);
});
}
})
.controller('doctorDetailsCtrl', function($scope) {
})
试试这个$ http.post( “HTTP://localhost/drmedic/register_user.php”,$ scope.users) .success(功能(响应){ alert(response.data); }) –
你做'警报(数据)'数据是一个对象,所以你的警报说“你告诉我警惕一个对象”。没有看到有什么不妥。也许你需要改变响应类型以防止JS将响应解析为JSON。 – apokryfos
谢谢。它帮助很多 –