【问题标题】:Passing an array to controller via ajax通过ajax将数组传递给控制器
【发布时间】: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


【解决方案1】:

您必须echo Controller 中的 JSON 编码数据,而不是返回它。在 Ajax 回调中,您必须在 $.each(..) 调用之前 JSON.parse() result

控制器

function getValues(){
    $playlist = $this->input->post('course');
    $this->load->model('get_db');
    $data = $this->get_db->getAll($playlist);//$var inside ()
    echo json_encode($data);
    // $this->output->set_content_type('application/json');
    // $this->output->set_output(json_encode($data));
    // return $data;
}

Ajax 调用

$.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 = '';
        var jsonData = JSON.parse(result);
        $.each(jsonData, function(a, b){
            htmlStr += b.qText + '<br />';
        });
        alert("YEAH YOU WAS SUCCESSFUL");
        $('#testing1').append(htmlStr);
    }

【讨论】:

  • 您好,感谢您的回复。因此,当我集成此代码时,我得到一个“未捕获的语法错误:意外的令牌 o”错误。我还担心当我进行硬编码以确保控制器和模型正常工作时,我使用的数组是一个 php 数组。但是,现在我正在尝试发送一个 javascript 数组。我想知道这是否与我所做的 php 编码相混淆。
  • 这将由 '$data' 的内容引起(解析时)。也许您可以提供更多信息,甚至可以提供更好的测试站点/小提琴,我可以在其中查找问题所在。 “结果”的内容也会有所帮助。
  • 哦,如果没有将 set_content_type(..) 注释掉,您可能不能在客户端解析结果以适合您的错误消息。
猜你喜欢
  • 1970-01-01
  • 2011-12-21
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 2014-02-10
  • 2015-08-13
相关资源
最近更新 更多