[英]Displaying returned json data from controller with AJAX
我正在嘗試將POST提交發送到控制器,並希望接收回json字符串,以便可以將信息附加到表中。 這是我到目前為止的內容:
我的AJAX電話:
$('#addTicketButton').click(function(){
$.ajax({
type:'POST',
url:'<?php echo site_url();?>ticket_system/add_ticket',
data:{
'headline': $("#headline").val(),
'description': $('#description').val(),
'category': $('#category').val(),
'priority': $('#priority').val(),
'assigned': $('#assign').val()
},
datatype: 'json',
success: function(data){
$("#created_table > tbody:last-child").append("<tr><td>" + data[0].ticketId + "</td><td>" + $("#headline").val() + "</td><td>" + data[0].lastUpdated + "</td></tr>");
}
});
});
我的控制器:
public function add_ticket()
{
$ticket = array(
'headline' => $this->input->post('headline'),
'description' => $this->input->post('description'),
'category' => $this->input->post('category'),
'priority' => $this->input->post('priority'),
'assigned' => $this->input->post('assigned'),
'userId' => $this->userId
);
$ticketId = $this->tickets->addTicket($ticket);
$ticket = $this->tickets->getTicketByTicketId($ticketId);
$success = array(
'ticketId' => $ticketId,
'lastUpdated' => date("M d, Y H:i A")
);
echo json_encode($success);
}
我也嘗試過使用:
data.ticketId
但這也不起作用。
任何幫助我將不勝感激。 謝謝!
不確定,但是您可以嘗試以下操作:
控制器:
$success = json_encode($ticketId||date("M d, Y H:i A")); //added '||' between the results
阿賈克斯:
d = data.split("||"); //break the results
$("#created_table > tbody:last-child").append("<tr><td>" + d[0] + "</td><td>" + $("#headline").val() + "</td><td>" + d[1] + "</td></tr>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.