简体   繁体   中英

how to get response from json ajax call in codeigniter view?

I am trying to get a response from the codeigniter controller in my view, but without success. Here is the code in my javascript file:

 // user enter his e-mail so check him against the database.
   $("#formSendPassword").submit(function(e){     

       e.preventDefault();

        var email = $(this).find("#checkemail").val();

        var obj = {email: email};
        var url = $(this).attr("action");
        var data =  {email: email};

        $.post(url, obj,data, function(jsonResp){
           console.log(success);
            if(jsonResp.success) {
                     alert(jsonResp['success']);
                    $('#successMailMessage').fadeIn();

          }  else {
                    alert("Fail");
                    $('#errorMailMessage').fadeIn();
                    }

        }, 'json');
    })

Code in my controller is as follow:

 public function checkEmail()
    {     
        // set the validation rules
        $this->form_validation->set_rules('checkemail', 'E-Mail', 'valid_email|required|trim|encode_php_tags');

        $this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
        // if validation is passed
        if ($this->form_validation->run() != FALSE) 
        {
            $ids=array();
            $ids[0]=$this->db->where('email', $this->input->post('checkemail'));

            $query = $this->backOfficeUsersModel->get();
            if($query)
            {

                $data = array(
                    'userid'       => $query[0]['userid'],
                    'username'       => $query[0]['username'],
                    'password'       => $query[0]['password'],
                    'firstname'       => $query[0]['firstname'],
                    'lastname'       => $query[0]['lastname'],
                    'email'       => $query[0]['email']
                ); 

                $jsonResp['success'] = "Ok";
                $jsonResp = array();
        } else {   
               // echo json_encode(array("success" => false, "error" => "Wrong email"));
                $jsonResp['success'] = "Fail";
        }
        //  form validation has failed 
        } else {     
            $errorMessage = "Please enter valid e-mail";
        }
    }   // end of function checkEmail 

As you can see, i am trying to console.log the success in my js file, but without success. Can anyone tell me what i am doing wrong?

Regards,Zoran

You should use a $data array element to store your json response before passing it to a view. This view is the json response your javascript callback is looking for. My guess would be that your console log call never fires.

Try the following:

In your Controller code something like:

 $data['json'] = json_encode(array("success" => false, "error" => "Wrong email"));
 $this->load->view('json_response', $data);

Define a view as follows:

<?php
    $this->output->set_header('Content-Type: application/json; charset=utf-8');
    echo $json;
?>

You should also define a datatype in the jQuery call (dataType: "json").

EDIT: So your jQuery post call should look as follows:

$.post(url, data, function(data) {
    console.log(data.sucess);
}, "json");

You don't need to pass obj as well. Furthermore you need to log data.success, not just success. Let me know

Your controller is setting the final data but you are not printing it out. I didn't find your array being converted to json using json_encode and then printing it out.

Try that out. Plus you are populating the arrray and then why are you again doing an = array() ?

$jsonResp['success'] = "Ok";
$jsonResp = array();

See if this link helps you: http://amitavroy.com/justread/content/articles/getting-ajax-data-using-views-codeigniter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM