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
謝謝。它幫助很多 –