codeigniter ajax json

aitupu-v1.3.0php\application\config
ucenter.php

$config = array (   'is_active' => 0,   'uc_host' => 'localhost',   'uc_dbuser' => 'root',   'uc_dbpw' => '111111',   'uc_dbname' => 'ucenter',   'uc_dbcharset' => 'utf8',   'uc_dbtablepre' => 'uc_',   'uc_key' => 'FDSFSDFDFWERETY565656RRET',   'uc_api' => 'http://127.0.0.6/ucenter/upload',   'uc_apiid' => '2', );


------------------------------------------------

//Only for AJAX  Controller
 public function ajax_register()
 {
  log_message('error','action ok');
  if(!is_ajax_request())//from helper
  exit('Access Denied!');
  if($this->input->post() && $this->validate_register_form()){
   log_message('error','post ok');
    $data['email'] = $this->input->post('email',true);
    $data['nickname'] = $this->input->post('nickname',true);
    $data['passwd'] = md5($this->input->post('password',true));
    $org_password = $this->input->post('password',true);
    $data['is_active'] = 1;
    if ($this->user_model->check_nickname_exists($data['nickname'])) {
     $response = array('result' => false, 'msg' => "用户昵称已经存在");
     echo json_encode($response);
     exit;
    }
    if ($this->user_model->check_email_exists($data['email'])) {
     $response = array('result' => false, 'msg' => "邮箱已经存在");
     echo json_encode($response);
     exit;
    }
    $this->config->load('ucenter',TRUE);
    $ucenter =  $this->config->item('ucenter');
    $uc = $ucenter['is_active'];
    if ($uc == 1) {//如果整合ucenter
     
     define('UC_CONNECT', 'mysql');
     define('UC_DBHOST', $ucenter['uc_host']);
     define('UC_DBUSER', $ucenter['uc_dbuser']);
     define('UC_DBPW', $ucenter['uc_dbpw']);
     define('UC_DBNAME', $ucenter['uc_dbname']);
     define('UC_DBCHARSET', $ucenter['uc_dbcharset']);
     define('UC_DBTABLEPRE', "`{$ucenter['uc_dbname']}`.{$ucenter['uc_dbtablepre']}");
     define('UC_DBCONNECT', '0');
     define('UC_KEY', $ucenter['uc_key']);
     define('UC_API', $ucenter['uc_api']);
     define('UC_CHARSET', 'utf-8');
     define('UC_IP', '');
     define('UC_APPID', $ucenter['uc_apiid']);
     define('UC_PPP', '20');
     
     require_once './uc_client/client.php';
     
     $uc_uid = uc_user_register($data['nickname'] , $org_password , $data['email']);
     
     if($uc_uid == -1){
      $response = array('result' => false, 'msg' => "用户名不合法");
      echo json_encode($response);
      exit;
     }elseif($uc_uid == -2){
      $response = array('result' => false, 'msg' => "包含要允许注册的词语");
      echo json_encode($response);
      exit;
     }elseif($uc_uid == -3){
      $response = array('result' => false, 'msg' => "用户名已经存在");
      echo json_encode($response);
      exit;
     }elseif($uc_uid == -4){
      $response = array('result' => false, 'msg' => "Email 格式有误");
      echo json_encode($response);
      exit;
     }elseif($uc_uid == -5){
      $response = array('result' => false, 'msg' => "Email 不允许注册");
      echo json_encode($response);
      exit;
     }elseif($uc_uid == -6){
      $response = array('result' => false, 'msg' => "该 Email 已经被注册");
      echo json_encode($response);
      exit;
     }
     
    }
    if($uid = $this->user_model->add_user($data)){
     $update_data['avatar_local'] = $this->user_model->create_default_avatar($uid);
     if($update_data['avatar_local']){
      $this->user_model->edit_user($uid,$update_data);
     }
     $user = $this->user_model->get_user_by_uid($uid);
     $this->user_model->set_usersession($user);
     if ($uc == 1) {
      $synlogin = uc_user_synlogin($uc_uid);
      $response = array('result' => true, 'msg' => "注册成功" , 'synlogin' => $synlogin);
     }else {
      $response = array('result' => true, 'msg' => "注册成功");
     }
     echo json_encode($response);
    }else{
     $response = array('result' => false, 'msg' => "注册失败,请检查你输入的内容是否正常");
     echo json_encode($response);
    }
  }else{
   $response = array('result' => false, 'msg' => "注册失败,请检查你输入的内容是否正常");
   echo json_encode($response);
  }
 }

...................................................................................................



js code
var item ="COOL!";
$.post("http://192.168.8.138/index.php/main/test",{"item": item },function(data){
            alert(data.result);},"json");



php class main extendsController{function test(){ $item = trim($this->input->post('item')); $array = array('result'=> $item); header('Content-Type: application/json',true); echo json_encode($array);}}?>)


//----------------------------------------------------------

n your controller, at the end off the function, try:

$this->output->set_header('Content-type: application/json; charset=UTF-8');
$this->output->set_output(json_encode($array));
//--------------------------------------------------
classSiteextends CI_Controller
{function __construct(){
        parent::__construct();
        $this->load->model('mUsers');}function index(){
        $this->load->view('home');}function read(){
        header('Content-Type: application/json',true); 
        echo json_encode($this->mUsers->getAll());}}

你可能感兴趣的:(codeigniter ajax json)