[AngularJS]json 回报 [对象] 提交点击

发布时间: 2017/4/16 18:37:31
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我通过表单详细信息从离子到 php 文件 throuugh 控制器。如果我输入表单数据直接通过邮递员,它获取存储。但我试试通过填写表单数据时出现此错误︰

无论何时,单击提交按钮,我会出现此错误的警报框。-[对象]

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:

提醒只返回一个对象的对象对象"

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

返回"对象"。如果你转到。

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

返回"啦"。 如果你需要你可以做。

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

这将返回 la1la2

官方微信
官方QQ群
31647020