500 错误状态:使用 angular js 和 codeigniter 2.2
500 error status : using angular js and codeigniter 2.2
刚开始学习angularjs.
我正在使用 codeigniter 2.2。
我有一页使用 angular js 显示 table 中的所有用户。在同一页上,我有 添加用户 表格。我想通过表单保存数据,使用 angular js。
打印机错误为:500(红色)
*数据未保存。
为什么会出现这种情况,如何解决。请做必要的。
谢谢。
以下是我的代码结构。
demo_angular.js
var angularapp = angular.module('angularapp', []);
// add user
angularapp.controller ('FrmController',function ($scope , $http) {
$scope.errors = [];
$scope.msgs = [];
$scope.addUser = function() {
$scope.errors.splice(0, $scope.errors.length); // remove all error messages
$scope.msgs.splice(0, $scope.msgs.length);
$http.post("../admin/users/add_user", {'fname': $scope.fname, 'lname': $scope.lname, 'uname': $scope.uname,'gender': $scope.gender,'email': $scope.email,'mobile': $scope.mobile,'pass': $scope.pass}
).success(function(data, status, headers, config) {
if (data.msg != '')
{
$scope.msgs.push(data.msg);
$scope.get_users();
}
else
{
$scope.errors.push(data.error);
}
}).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
$scope.errors.push(status);
});
}
});
查看:
<form name="form_add" method="post" ng-controller="FrmController">
<ul>
<li style="color:red" ng-repeat="error in errors"> {{ error}} </li>
</ul>
<ul>
<li style="color:green" ng-repeat="msg in msgs"> {{ msg}} </li>
</ul>
<table class="table" cellspacing=10 cellpadding=10>
<tr>
<td>First Name: <input required type="text" ng-pattern="/[a-zA-Z]$/" ng-model="fname" name="fname" id="fname" value=""/><br/>
<span style="color:red" ng-show="form_add.fname.$dirty && form_add.fname.$invalid">
<span ng-show="form_add.fname.$error.required">First Name is required.</span>
<span ng-show="form_add.fname.$error.pattern">Invalid</span>
</span>
</td>
<td>Last Name: <input required type="text" ng-pattern="/[a-zA-Z]$/" ng-model="lname" name="lname" id="lname" value=""/><br/>
<span style="color:red" ng-show="form_add.lname.$dirty && form_add.lname.$invalid">
<span ng-show="form_add.lname.$error.required">Last Name is required.</span>
<span ng-show="form_add.lname.$error.pattern">Invalid</span>
</span>
</td>
<td>User Name: <input required type="text" ng-pattern="/^[a-zA-Z0-9]{6,15}$/" ng-model="uname" name="uname" id="uname" value=""/><br/>
<span style="color:red" ng-show="form_add.uname.$dirty && form_add.uname.$invalid">
<span ng-show="form_add.uname.$error.required">User Name is required.</span>
<span ng-show="form_add.uname.$error.pattern">Invalid: Minimum 6 characters and maximum 15 characters</span>
</span>
</td>
<td width="20%">Gender :<br/> <input type="radio" ng-model="gender" name="gender" id="r1" value="m" checked/>Male
<input type="radio" ng-model="gender" name="gender" id="r2" value="f"/>Female</td>
</tr>
<tr>
<td>Email ID: <input type="text" required ng-pattern="/^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$/" ng-model="email" name="email" id="email" value=""/><br/>
<span style="color:red" ng-show="form_add.email.$dirty && form_add.email.$invalid">
<span ng-show="form_add.email.$error.required">Email is required.</span>
<span ng-show="form_add.email.$error.pattern">Invalid email address.</span>
</span>
</td>
<td>Mobile : <input type="text" required ng-model="mobile" ng-pattern="/^[1-9]{1}[0-9]{9}$/" name="mobile" id="mobile" value=""/><br/>
<span style="color:red" ng-show="form_add.mobile.$dirty && form_add.mobile.$invalid">
<span ng-show="form_add.mobile.$error.required">Mobile is required.</span>
<span ng-show="form_add.mobile.$error.pattern">10 digits required</span>
</span>
</td>
<td>Password : <input required type="password" ng-model="pass" name="pass" id="pass" value=""/>
<span style="color:red" ng-show="form_add.mobile.$dirty && form_add.mobile.$invalid">
<span ng-show="form_add.mobile.$error.required">Mobile is required.</span>
<span ng-show="form_add.mobile.$error.pattern">10 digits required</span>
</span>
</td>
<td><button type="button" class="btn btn-primary" ng-click='addUser();'>Save</button></td>
</tr>
</table>
<br/>
</form>
Codeigniter 控制器函数:
public function add_user()
{
$data = json_decode(file_get_contents("php://input"));
// print_r($data); exit;
$my_data['fname']=$data->fname;
$my_data['lname']=$data->lname;
$my_data['uname']=$data->uname;
$my_data['gender']=$data->gender;
$my_data['email']=$data->email;
$my_data['mobile']=$data->mobile;
$my_data['pass']=$data->pass;
$result=$this->vanesh_model->add_user($my_data);
if ($result)
{
$arr = array('msg' => "User Created Successfully!!!", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
}
else
{
$arr = array('msg' => "", 'error' => 'Error In inserting record');
$jsn = json_encode($arr);
print_r($jsn);
}
}
型号:
public function add_user($my_data)
{
$this->db->insert('user', $my_data);
return $this->db->insert_id();
}
已编辑:
// 查看所有用户
angularapp.controller('angular_controller',function ($scope , $http) {
$scope.orderByField = 'fname';
$scope.get_users = function() {
$http.get("../admin/users/angular_all_users").success(function(data)
{
$scope.allUsers = data;
});
}
});
我认为问题出在你的 $my_data array creation
像 your controller
中那样创建数组
$my_data = array(
'lname' => $data->lname,
'uname' => $data->uname,
'gender' => $data->gender,
'email' => $data->email,
'mobile' => $data->mobile,
'pass' => $data->pass
);
和
Check your console and use die() and print() step by step and find out
from where it comes 500
刚开始学习angularjs. 我正在使用 codeigniter 2.2。 我有一页使用 angular js 显示 table 中的所有用户。在同一页上,我有 添加用户 表格。我想通过表单保存数据,使用 angular js。 打印机错误为:500(红色) *数据未保存。 为什么会出现这种情况,如何解决。请做必要的。 谢谢。
以下是我的代码结构。
demo_angular.js
var angularapp = angular.module('angularapp', []);
// add user
angularapp.controller ('FrmController',function ($scope , $http) {
$scope.errors = [];
$scope.msgs = [];
$scope.addUser = function() {
$scope.errors.splice(0, $scope.errors.length); // remove all error messages
$scope.msgs.splice(0, $scope.msgs.length);
$http.post("../admin/users/add_user", {'fname': $scope.fname, 'lname': $scope.lname, 'uname': $scope.uname,'gender': $scope.gender,'email': $scope.email,'mobile': $scope.mobile,'pass': $scope.pass}
).success(function(data, status, headers, config) {
if (data.msg != '')
{
$scope.msgs.push(data.msg);
$scope.get_users();
}
else
{
$scope.errors.push(data.error);
}
}).error(function(data, status) { // called asynchronously if an error occurs
// or server returns response with an error status.
$scope.errors.push(status);
});
}
});
查看:
<form name="form_add" method="post" ng-controller="FrmController">
<ul>
<li style="color:red" ng-repeat="error in errors"> {{ error}} </li>
</ul>
<ul>
<li style="color:green" ng-repeat="msg in msgs"> {{ msg}} </li>
</ul>
<table class="table" cellspacing=10 cellpadding=10>
<tr>
<td>First Name: <input required type="text" ng-pattern="/[a-zA-Z]$/" ng-model="fname" name="fname" id="fname" value=""/><br/>
<span style="color:red" ng-show="form_add.fname.$dirty && form_add.fname.$invalid">
<span ng-show="form_add.fname.$error.required">First Name is required.</span>
<span ng-show="form_add.fname.$error.pattern">Invalid</span>
</span>
</td>
<td>Last Name: <input required type="text" ng-pattern="/[a-zA-Z]$/" ng-model="lname" name="lname" id="lname" value=""/><br/>
<span style="color:red" ng-show="form_add.lname.$dirty && form_add.lname.$invalid">
<span ng-show="form_add.lname.$error.required">Last Name is required.</span>
<span ng-show="form_add.lname.$error.pattern">Invalid</span>
</span>
</td>
<td>User Name: <input required type="text" ng-pattern="/^[a-zA-Z0-9]{6,15}$/" ng-model="uname" name="uname" id="uname" value=""/><br/>
<span style="color:red" ng-show="form_add.uname.$dirty && form_add.uname.$invalid">
<span ng-show="form_add.uname.$error.required">User Name is required.</span>
<span ng-show="form_add.uname.$error.pattern">Invalid: Minimum 6 characters and maximum 15 characters</span>
</span>
</td>
<td width="20%">Gender :<br/> <input type="radio" ng-model="gender" name="gender" id="r1" value="m" checked/>Male
<input type="radio" ng-model="gender" name="gender" id="r2" value="f"/>Female</td>
</tr>
<tr>
<td>Email ID: <input type="text" required ng-pattern="/^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$/" ng-model="email" name="email" id="email" value=""/><br/>
<span style="color:red" ng-show="form_add.email.$dirty && form_add.email.$invalid">
<span ng-show="form_add.email.$error.required">Email is required.</span>
<span ng-show="form_add.email.$error.pattern">Invalid email address.</span>
</span>
</td>
<td>Mobile : <input type="text" required ng-model="mobile" ng-pattern="/^[1-9]{1}[0-9]{9}$/" name="mobile" id="mobile" value=""/><br/>
<span style="color:red" ng-show="form_add.mobile.$dirty && form_add.mobile.$invalid">
<span ng-show="form_add.mobile.$error.required">Mobile is required.</span>
<span ng-show="form_add.mobile.$error.pattern">10 digits required</span>
</span>
</td>
<td>Password : <input required type="password" ng-model="pass" name="pass" id="pass" value=""/>
<span style="color:red" ng-show="form_add.mobile.$dirty && form_add.mobile.$invalid">
<span ng-show="form_add.mobile.$error.required">Mobile is required.</span>
<span ng-show="form_add.mobile.$error.pattern">10 digits required</span>
</span>
</td>
<td><button type="button" class="btn btn-primary" ng-click='addUser();'>Save</button></td>
</tr>
</table>
<br/>
</form>
Codeigniter 控制器函数:
public function add_user()
{
$data = json_decode(file_get_contents("php://input"));
// print_r($data); exit;
$my_data['fname']=$data->fname;
$my_data['lname']=$data->lname;
$my_data['uname']=$data->uname;
$my_data['gender']=$data->gender;
$my_data['email']=$data->email;
$my_data['mobile']=$data->mobile;
$my_data['pass']=$data->pass;
$result=$this->vanesh_model->add_user($my_data);
if ($result)
{
$arr = array('msg' => "User Created Successfully!!!", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
}
else
{
$arr = array('msg' => "", 'error' => 'Error In inserting record');
$jsn = json_encode($arr);
print_r($jsn);
}
}
型号:
public function add_user($my_data)
{
$this->db->insert('user', $my_data);
return $this->db->insert_id();
}
已编辑: // 查看所有用户
angularapp.controller('angular_controller',function ($scope , $http) {
$scope.orderByField = 'fname';
$scope.get_users = function() {
$http.get("../admin/users/angular_all_users").success(function(data)
{
$scope.allUsers = data;
});
}
});
我认为问题出在你的 $my_data array creation
像 your controller
$my_data = array(
'lname' => $data->lname,
'uname' => $data->uname,
'gender' => $data->gender,
'email' => $data->email,
'mobile' => $data->mobile,
'pass' => $data->pass
);
和
Check your console and use die() and print() step by step and find out from where it comes 500