控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ajax_process extends CI_Controller {
public function __construct() {
parent::__construct();
ini_set('session.cookie_httponly', 1);
ini_set('session.use_only_cookies', 1);
$this->load->model('users_class');
}
public function index()
{
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
try {
$post = $this->input->post();
switch($post['act']) {
case 'register':
$callback = $this->users_class->insert($post);
break;
default:
$callback = array('status'=>'Invalid action');
}
echo json_encode($callback);
}
catch(Exception $e) {
echo json_encode(array('status'=>$e));
}
}
}
模型
if( $chk_strUserName === TRUE )
{
$msg['status'] = 'username_exist';
}
else if( $chk_strEmail === TRUE )
{
$msg['status'] = 'email_exist';
}
else if( !valid_email($strEmail) )
{
$msg['status'] = 'email_not_valid';
}
else
{
$query = $this->db->insert('users', $data);
if($this->db->_error_number()==0){
$msg['status'] = 'success';
}
else
{
$msg['status'] = 'error';
}
}
return $msg;
AJAX
var strFirstName = $('#strFirstName').val();
var strLastName = $('#strLastName').val();
var strEmail = $('#strEmail').val();
var strPassword = $('#strPassword').val();
var strGender = $('#strGender').val();
uri = 'ajax_process';
data = {
'act' : 'register',
'strFirstName' : strFirstName,
'strLastName' : strLastName,
'strEmail' : strEmail,
'strPassword' : strPassword,
'strGender' : strGender
};
$.ajax({
type: "POST",
url: base_url+uri,
data: data,
dataType: 'json',
success: function(result) {
console.log(result);
}
});
任何人都可以帮我找到程序中可能存在的错误/错误。
它正在工作,它将数据存储在我的数据库中,但我无法从json接收任何回调