【问题标题】:Sending array to controller/model in CI with AJAX使用 AJAX 将数组发送到 CI 中的控制器/模型
【发布时间】:2014-01-12 22:59:58
【问题描述】:

所以基本上我在 javascript 中有一个两项数组,它是根据用户单击的链接生成的。我正在尝试将此数组发送到我的控制器,然后发送到模型。模型将查看数组并将其中一项与特定列/行匹配,并选择性地显示数据。

我在将数据从 AJAX 传递到我的控制器时遇到问题。

AJAX 代码(在 .js 文件中)

  $('#youmax-video-list-div').append('<div id="testing1"></div>');

                    $('#concept').click(function(){
                                        console.log(course);
                       $.ajax({
                              url: 'http://localhost:8888/index.php/trial/getValues',
                              type:'POST',
                              dataType: 'json',
                              data:'',
                              error: function(){
                              $('#testing1').append('<p>goodbye world</p>');
                              },
                              success: function(result) {
                              var htmlStr = '';
                              $.each(result, function(a, b){
                                     htmlStr += b.qText + '<br />';
                                     });
                              $('#testing1').append(htmlStr);
                              } // End of success function of ajax form
                              }); // End of ajax call
                       });

我假设我必须对数据字段做一些事情,但我不知道它的语法。

chrome 中的控制台登录出现了这个:

数组[2] 0:“PL8G0kdHFfE3WuyevUDvwSYCZw8mp8LBFA” 1:“PL8G0kdHFfE3V62Jp2ju-PelOaNUkH7xR8”

控制器

function getValues(){
    $this->load->model('get_db');
    $data = $this->get_db->getAll();
    $this->output->set_content_type('application/json');
    $this->output->set_output(json_encode($data));
    return $data;  
}

型号

class Get_db extends CI_Model{
    function getAll(){
        $query=$this->db->query("SELECT * FROM questions");
        return $query->result();
        //returns from this string in the db, converts it into an array

    }
}

正如我之前所说,我真的只关心将数据发送到我的控制器和模型。 数组的名称是“课程”。但是,console.log 中似乎并非如此。这只是 chrome 的作用吗?

【问题讨论】:

    标签: ajax codeigniter model controller


    【解决方案1】:

    // chkval 是数组元素的类

    AJAX 函数:

    function ajaxSub(goals)
    {
        var theArray = new Array();
        var i=0;
        jQuery('.chkval').each(function(){
            if(jQuery(this).prop('checked'))
            {
                theArray[i] = jQuery(this).val();
                i++;
            }
        });
        jQuery.ajax({
             url: '<?php echo site_url('mycontroller/myfunctions');?>',
             type: 'post',
             data: {
                 arr: theArray,
                 other_id: ''
             },
             datatype: 'json',
             success: function () {
             }
        });
    }
    

    在控制器中:

    $arr = $this->post('arr');
    
    foreach($arr as $ar)
    {
        echo $ar; // prints each element of the array.
    }
    

    【讨论】:

    • 我尝试了这个解决方案,但没有结果。每当我使用 echo 和 ajax 时,都会出现我在 ajax 调用中设置的错误消息。但是,如果我将 echo 更改为返回,我会获得成功功能,但仍然没有显示任何内容 @ReNiShAR
    • 在上面我只是将它作为一个例子,你可以对数组做任何事情并返回......
    【解决方案2】:

    与上述答案相同,但在控制器中运行此代码:

    $arr = $this->input->post('arr');
    
    foreach($arr as $ar)
    {
    
        echo $ar; // prints each element of the array.
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2018-03-05
      相关资源
      最近更新 更多