[英]Cannot display returned JSON from a JQUERY AJAX call in CodeIgniter
我正在使用JQUERY + CodeIgniter。 我似乎無法在ajax調用后顯示返回的data
。
這是我的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");
這是我的CodeIgniter:
function get_conflict() {
...
log_message("debug","get_conflict(): $result");
return $result;
}
我的日志顯示:
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}
表示已正確返回JSON。 但是,不會顯示alert(data)
或alert(data.work_product_name)
。
有任何想法嗎?
您正在返回結果...您應該將其輸出。
例如
function get_conflict() {
...
log_message("debug","get_conflict(): $result");
$this->output->set_output( json_encode($result) );
}
嘗試這個
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);
}
}
和腳本
<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;
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.