CodeIgniter JSON响应问题

时间:2014-10-14 18:36:56

标签: php jquery ajax json codeigniter

我遇到了一个我一直在研究的控制面板问题。我有一个常规帐户设置页面。在此页面上,用户可以执行各种功能,例如创建新用户,更改密码或更新其电子邮件首选项。

帐户创建面板无任何问题。数据获取POST,并返回JSON响应。

我在下一个用于更改用户密码的面板上遇到困难。 数据获得POST,Firebug获得相应的响应。 Chrome Developer JS控制台什么都没有,但数据仍然会被发布。没有任何消息出现在他们应该在HTML中的位置。因此,该功能正常工作,如果您更改密码并刷新页面,则会从JSON响应中获取消息。没有报告服务器或php错误。

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends MY_Controller {

public function __construct() {
        parent::__construct();
}

public function index()
{
    if ($this->input->is_ajax_request()) {
                $formData = array();
                parse_str($this->input->post('formData'), $formData);
                $action = $formData['action'];
                switch($action){
                    case("createAccount"):
                        $this->createAccount($this->input->post('formData'));
                        break;
                    case("changePassword");
                        echo "Changing password...";
                        $this->changePassword($this->input->post('formData'));
                        break;
                    case("updateEmail");
                        echo "Updating Email settings...";
                        $this->updateEmail($this->input->post('formData'));
                }
             }
             else {
                 redirect('/', 'refresh');
             }
}

    public function createAccount($data)
    {
        parse_str($data, $_POST); 
        $this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
        $this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
        $this->form_validation->set_rules('company', 'Company', 'required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
        $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
       if ($this->form_validation->run() == FALSE)
        {
               $errors = validation_errors();
               $response = array("response" => "400",
                                 "message" => $errors);
        }
        else
        {
        $additional_data = array("first_name" => $_POST['first_name'], "last_name" => $_POST["last_name"], "company" => $_POST['company'], "phone" => $_POST["phone"]);
        $group_ids = array("2" => "2");

            $account = $this->ion_auth->register($_POST['email'], $_POST['password_confirm'], $_POST['email'], $additional_data, $group_ids);
            if($account){
                $response = array("response" => "200",
                                  "message" => "Account Created Successfully", 
                                  "user" => $_POST['email']);
            }
            else {
                $response = array("response" => "400",
                                  "message" => "This account already exists", 
                                  "user" => $_POST['email']);
            }
        }
        $this->json_output($response);
    }

     public function changePassword($data)
    {
     parse_str($data, $_POST);
       $this->form_validation->set_rules('old', 'Old Password', 'required|xss_clean');
       $this->form_validation->set_rules('new', 'New Password', 'required|xss_clean');
       $this->form_validation->set_rules('new_confirm', 'Confirm New Password', 'required|xss_clean');
        if ($this->form_validation->run() == FALSE)
        {
               $errors = validation_errors();
               $response = array("response" => "400",
                                 "message" => $errors);
        }
        else
        {
                    $identity = $this->session->userdata('identity');
        $change = $this->ion_auth->change_password($identity, $this->input->post('old'), $this->input->post('new'));
        if ($change)
        {
                            $errors = validation_errors();
                            $response = array("response" => "200",
                                              "message" => "Password Successfully Changed");
        }
        else
        {
                            $errors = validation_errors();
                            $response = array("response" => "400",
                                              "message" => $this->session->set_flashdata('message', $this->ion_auth->errors()));

        } 
        }
        $this->json_output($response);
    }
 public function json_output($data)
    {
        $this->output
                        ->set_content_type('application/json')
                        ->set_output(json_encode($data))->_display();
        exit;
    }
}

的jQuery

$("#createAccount").click(function(){
    var formData =  $("#createAccountForm").serialize();
    $.post(base_url + "ajax/index", {
        dataType: "json",
        formData : formData,
        cahce: false
    }, function(data) {
        if(data["response"] == "200")
        {
            $("#modalTitle").html("<i class=\"fa fa-user\"></i><sup><i class=\"fa fa-plus\"></i></sup> Client Account Created");
            $("#modalBody").html("<p>The user account for <strong>" + data["user"] + "</strong> has been created.");
            $("#modalFooter").html("<a href='#' data-dismiss='modal' class='btn btn-info'>OK</a>");
            $("#createAccountMsg").html("");
            $('#notificationModal').modal('show');
        }
        if(data["response"] == "400")
        {
            $("#createAccountMsg").html("<div class='panel panel-danger'><div class='panel panel-heading'><h3><i class='fa fa-exclamation'></i> Error Creating User</h3></div><div class='panel-body'>"+data["message"]+"</div></div>")
        }
    });
    return false;
});

$("#changePassword").click(function(){
   var formData = $("#changePasswordForm").serialize();
   $.post(base_url + "ajax/index", {
       dataType: "json",
       formData: formData,
       cahce: false
   }, function(data) {
      if(data["response"] == "200")
        {
            $("#modalTitle").html("<i class=\"fa fa-gear\"></i></sup> Password Changed");
            $("#modalBody").html("<p>Your password has been successfully changed.");
            $("#modalFooter").html("<a href='#' data-dismiss='modal' class='btn btn-info'>OK</a>");
            $("#createAccountMsg").html("");
            $('#notificationModal').modal('show');
        }
        if(data["response"] == "400")
        {
            $("#changePasswordMsg").html("<div class='panel panel-danger'><div class='panel panel-heading'><h3><i class='fa fa-exclamation'></i> Error Changing Password</h3></div><div class='panel-body'>"+data["message"]+"</div></div>")
        }
   });
    return false;
}); 
}); 

0 个答案:

没有答案