codeIgniter Ajax响应中包含的数据,但输出不确定不确定、数据、codeIgniter、Ajax

2023-09-10 18:12:53 作者:消逝在记忆

我一直在codeIgniter项目,并甲肝遇到一个问题,使用Ajax从我的控制器返回的数据。

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

控制器:

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

型号:

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

        return $query->result();
}

在视图的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 />');

    });
}
      });

});

我可以输出使用PHP foreach循环,但现在我想将其转换成用ajax数据的罚款。如果我是正确的,数据是输出为数组对象的。 每一个对象包含数据的6左右的领域,如:的ID,名称,网址,相关的摘录,源代码的

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.

我是相当新的使用AJAX的任何东西,但在我尝试使用Chrome检查调试问题,并检查网络,我可以看到Ajax调用和响应选项卡:我需要在这里显示的数据以这种方式:

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:

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

由于数据显示在反应,但要么没有显示在页面上,或者如果我改变了ajax调用我可以得到它的输出UNDEFINED。

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调用数据的页面上约10个左右的div。这使我的身边的问题,以及...

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...

我的模型返回50行从数据库中的数据。我目前通过这一切来的AJAX功能以JSON格式。我将只使用10行之后的数据的最初,然后使用行的其余部分上的时间在该网页上所确定的量。它是最好继续输出所有50行到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?

我已经通过一些AJAX教程与CI,但是,所有的人都使用后跑了,我还没有找到一个对的document.ready并且无需用户交互运行。我还注意到,必须有几个不同的方式来输出该数据。我已经看到了使用.append()。经过(),并以这种方式使用。价值()的,似乎没有针对我的具体情况,虽然工作...

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...

感谢您的帮助!

推荐答案

如果您的数据只是一个数组,你的code会工作。但是你的数据阵列包含在结果这是一个对象的属性。

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.

修改每个来:

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

或者干脆送阵列时没有把球送入PHP 结果阵列

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