0

Im using JQUERY + CodeIgniter. I can't seem to get the returned data to display after an ajax call.

Here is my JQUERY:

  $.post("<?= site_url('plan/get_conflict') ?>", {
    user_id : user_id,
    datetime_from : plan_datetime_start,
    datetime_to : plan_datetime_end,
    json : true
   }, function(data) {
    alert(data);
     }, "json");

Here is my CodeIgniter:

function get_conflict() {
    ...
 log_message("debug","get_conflict(): $result");
 return $result;
}

My logs show:

get_conflict(): {"work_product_name":"Functional Design Document","datetime_start_plan":"2010-04-22 08:00:00","datetime_end_plan":"2010-04-22 09:00:00","overlap_seconds":3600}

Meaning the JSON is being returned correctly. However, the alert(data) nor alert(data.work_product_name) are not displayed.

Any ideas?

2 Answers 2

3

You are returning the result... you should be outputting it.

For example

function get_conflict() {
    ...
    log_message("debug","get_conflict(): $result");

    $this->output->set_output( json_encode($result) );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

 public function CreateStudentsAjax() {

        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('', '');

        $this->form_validation->set_rules('roll', 'Roll Number', 'required');
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('phone', 'Phone', 'required');

        if ($this->form_validation->run()) {

            $this->welcome_model->InsertStudents();
            echo json_encode("Oks");
        } else {

            $data = array(
                'roll' => form_error('roll'),
                'name' => form_error('name'),
                'phone' => form_error('phone')
            );

            echo json_encode($data);
        }
    }

and Scripts

<script type="text/javascript">

            $(document).ready(function(){

                $('form').submit(function(){
                    //alert('ok');      
                    $.ajax({
                        url:this.action,
                        type:this.method,
                        data:$(this).serialize(),
                        success:function(data){
                            var obj = $.parseJSON(data);

                            if(obj['roll']!=null)
                            {                               
                                $('#message').text("");
                                $('#message').html(obj['roll']);
                                $('#message').append(obj['name']);
                                $('#message').append(obj['phone']);
                            }
                            else
                            {                               
                                $('#message').text("");
                                $('#message').html(obj); 
                            }

                        },
                        erro:function(){
                            alert("Please Try Again");
                        }                        
                    });
                    return false;
                });                        
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.