【发布时间】:2014-01-15 19:05:04
【问题描述】:
所以我使用 AJAX 将数组发送到控制器
AJAX
$('#concept').click(function(){
$.ajax({
url: 'http://localhost:8888/index.php/trial/getValues',
type:'POST',
dataType: 'json',
data: course,
error: function(){
$('#testing1').append('<p>goodbye world</p>');
},
success: function(result) {
var htmlStr = '';
$.each(result, function(a, b){
htmlStr += b.qText + '<br />';
});
alert("YEAH YOU WAS SUCCESSFUL");
$('#testing1').append(htmlStr);
} // End of success function of ajax form
}); // End of ajax call
course 是一个包含两个字符串的 javascript 数组。
控制器
function getValues(){
$playlist = $this->input->post('course');
$this->load->model('get_db');
$data = $this->get_db->getAll($playlist);//$var inside ()
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($data));
return $data;
}
型号
class Get_db extends CI_Model{
function getAll($pList){
$query=$this->db->query("SELECT * FROM questions WHERE playlistID= '$pList[0]' OR playlistID='$pList[1]'");
return $query->result();
}
}
我已经完成了硬编码,并且可以确认控制器和模型功能可以正常工作。我认为问题出在 ajax 中的“数据”参数上。我只是不相信数据以正确的方式传递给控制器。我无休止地搜索其他问题,我知道有类似的问题,但我没有找到这种情况下的解决方案。
提前感谢您提供的任何信息:)
【问题讨论】:
-
先尝试编码为 JSON。我不确定这是否是必需的,但它可能会让你的事情变得更简单。
-
我将如何以及在哪里这样做
-
我不确定我是否遗漏了什么,但根据您的代码,模型在查询中执行此操作:
..where playlist = array(1,2,3)。您不应该使用其他 codeigniter 函数,例如where_in还是先提取数组? -
@ISuthanBala 是的,请查看编辑
-
我还看到,您正在控制器中返回数据,而不是回显。只有回显的数据才会显示并返回给 ajax 调用
标签: ajax codeigniter post