CodeIgniter Ajax响应包含数据,但未定义输出。

时间:2022-01-10 19:26:15

I have been working on a CodeIgniter project and hav run into an issue with using ajax to return the data from my controller.

我一直在做一个CodeIgniter项目,hav遇到了使用ajax从我的控制器返回数据的问题。

Controller:

控制器:

function outputAjax()
    {
        $this->load->model('my_model');
        $data['results'] = $this->site_model->getInfo();
        $this->output->set_output(json_encode($data));
    }

Model:

模型:

function getInfo()
{
    $this->db->order_by('PubDate','DESC');
    $query = $this->db->get('Articles', 50);

        return $query->result();
}

Ajax function in View:

Ajax功能视图:

<div id="article-area">
    <p>Hey this is where the ajax call should output!</p>

</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" language="Javascript">
 jQuery(document).ready(function(){     
      $.ajax({
url: 'http://localhost/project/index.php/my_controller/outputAjax',
dataType:'json',
success: function(data) {
    $.each(data, function(index,item){
        $("#article-area").append('<div><b>' + item.id + '</b></div><hr />');

    });
}
      });

});

I can output the data fine using PHP foreach loop but now I am trying to convert that into using ajax. If I am correct, the data being output is an array of objects. Each 'object' contains 6 or so fields of data such as: id, title, url, PubDate, Source.

我可以使用PHP foreach循环很好地输出数据,但是现在我正在尝试将其转换为使用ajax。如果我是正确的,那么输出的数据就是一个对象数组。每个“对象”包含6个左右的数据字段,比如:id、标题、url、PubDate、Source。

I am fairly new to using ajax for anything but in my attempt to debug the issue, using Chrome inspect and checking 'Network' I can see the ajax call and in the response tab: all of the data I need is shown here in this way:

我对使用ajax做任何事情都很陌生,但在我试图调试这个问题时,使用Chrome检查和检查“Network”,我可以看到ajax调用,在response选项卡中:我需要的所有数据都显示在这里:

{"results":[{"id":"1","Source":"My Source","Title":"My Title". . . .

Since the data is shown in the response but either nothing shows up on the page or if I change the ajax call I can get it to output UNDEFINED.

由于数据显示在响应中,但是页面上没有显示任何内容,或者如果我更改ajax调用,我可以让它输出未定义的内容。

The final result I am looking to achieve is about 10 or so divs on the page with data from the ajax call. Which brings up my side question as well...

我希望实现的最终结果是页面上大约有10个div,其中包含来自ajax调用的数据。这也引出了我的另一个问题……

My model returns 50 rows of data from the db. I am currently passing all of it to the ajax function in json format. I will only be using 10 rows of that data initially and then use the rest of the rows over a determined amount of time on that page. Is it best to continue to output all 50 rows to the ajax call initially and then use it as needed, or limit the data to only what will be used initially from the model?

我的模型从数据库中返回50行数据。目前,我将所有这些内容都以json格式传递给ajax函数。我最初只使用10行数据,然后在页面上确定的时间内使用其余的行。最好是继续将所有的50行输出到ajax调用,然后根据需要使用它,或者将数据限制为最初从模型中使用的数据?

I have ran through a few ajax tutorials with CI but, all of them are using a POST and I have yet to find one that is running on document.ready and without user interaction. I also noticed there must be a few various ways to output the data. I have seen the use of .append() .after() and .value() used in this way, none seemed to work for my particular case though...

我已经浏览了一些使用CI的ajax教程,但是,它们都在使用一个POST,我还没有找到一个正在文档上运行的。准备好并且没有用户交互。我还注意到,必须有几种不同的方法来输出数据。我已经看到了.append() .after()和.value()的用法,尽管如此,似乎没有一种方法适合我的特定情况……

Thanks for the help!

谢谢你的帮助!

1 个解决方案

#1


3  

If your data was simply an array, your code would work. However your data array is contained in an results which is property of an object.

如果您的数据只是一个数组,那么您的代码就可以工作了。但是,数据数组包含在一个对象的属性结果中。

Change each to:

每一个改变为:

$.each(data.results, function(index,item){...

Or simply send the array without putting it into the php results array

或者只是发送数组而不将其放入php结果数组

$data = $this->site_model->getInfo();

#1


3  

If your data was simply an array, your code would work. However your data array is contained in an results which is property of an object.

如果您的数据只是一个数组,那么您的代码就可以工作了。但是,数据数组包含在一个对象的属性结果中。

Change each to:

每一个改变为:

$.each(data.results, function(index,item){...

Or simply send the array without putting it into the php results array

或者只是发送数组而不将其放入php结果数组

$data = $this->site_model->getInfo();