json在提交点击时返回[对象对象]

时间:2016-03-02 11:39:01

标签: php angularjs ionic-framework

我通过控制器将表格细节从离子传递到php文件。如果我直接通过邮递员输入表单数据,它就会被存储。但是当我通过填写表单数据来尝试它时,我得到了这个错误:

每当我点击提交按钮时,我会在警告框中收到此错误.- [object object]

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) {

})

1 个答案:

答案 0 :(得分:2)

警告对象只返回&#39; object Object&#34;

var bla = {
    'la1': 'la',
    'la2': 'la'
};
alert(bla);

返回&#34; object object&#34;。如果你转向。

var bla = {
    'la1': 'la',
    'la2': 'la'
};
alert(bla['la1']);

返回&#34; la&#34;。 如果你需要,你可以做。

var string = '';
var bla = {
    'la1': 'la1',
    'la2': 'la2'
};
for (var i = 1; i <= 2; i++) {
    string += bla['la'+i];
}
alert(string);

这将返回la1la2