Welcome Guest, Not a member yet? Register   Sign In
JSON Response Issue
#1

[eluser]J4CK[/eluser]
I am having an issue with a Control Panel that I've been working on. I have a general account settings page. On this page, the user can perform various functions such as create a new user, change their password, or update their e-mail preferences.

The account creation panel works without any issues. The data gets POST, and returns a JSON response.

Where I'm having difficult is on the next panel for changing the user's password. The data gets POST, and Firebug gets the appropriate response. However, the Chrome Developer JS console gets nothing back, but the data still gets posted to the database. None of the messages appear where they're supposed to in HTML (i.e. "You have changed your password").

So the function is working properly, if you change your password and refresh the page you get the message from the JSON response. No server or php errors are reported. The problem is the JSON response returned never gets shown. I was getting a headers already sent message, but I can't replicate it (have revised the code numerous times since then). Any help or advice would be appreciated. Thanks!

Controller - Code That Works (returns JSON)

Code:
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);
        }

Controller - Code That Doesn't Work

Code:
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" => "Original password is incorrect");
                              
   }
            }
            $this->json_output($response);
        }
        private function json_output($data)
        {
            $this->output
                            ->set_content_type('application/json')
                            ->set_output(json_encode($data))->_display();
            exit;
        }

jQuery - Broken AJAX Call

Code:
$("#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;
});
#2

[eluser]CroNiX[/eluser]
I see that you misspelled "cache" in the jQuery call.

Also, what happens if you console.log(data) as the first thing in the ajax success event?
#3

[eluser]J4CK[/eluser]
Hi CroNiX thanks for the typo correction.

Nothing is logged if I try console.log(data)

Nothing is returned at all, but the function executes, and Firebug registers the POST with the appropriate, but the response fails to come back and execute on the page.
#4

[eluser]CroNiX[/eluser]
Code:
private function json_output($data)
{
            $this->output
                            ->set_content_type('application/json')
                            ->set_output(json_encode($data))->_display();
            exit;
}

What's the _display() method?
#5

[eluser]J4CK[/eluser]
Suggestion from this StackOverflow thread:

http://stackoverflow.com/questions/12210...5_12210144
#6

[eluser]CroNiX[/eluser]
Just try this and see if you get data back to your jquery:

Code:
private function json_output($data)
{
   header('Content-type: application/json');
   echo json_encode($data);
}




Theme © iAndrew 2016 - Forum software by © MyBB