Reputation: 3205
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?
Upvotes: 0
Views: 1523
Reputation: 3017
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;
});
});
Upvotes: 0
Reputation: 1259
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) );
}
Upvotes: 3