duansai1314 2013-05-20 10:50
浏览 153
已采纳

AJAX调用在本地服务器上工作但不在线

When a user goes to one of the pages (let's call it page1), the PHP loads the HTML content for an array containing data about the users.

Once the page is loaded (DOM ready), I use jQuery to perform an AJAX call to retrieve the HTML for that array of data. I do this to get the benefit of using separate PHP template files. In this way, PHP will call the PHP template for every array in the bi-dimensionnal array and return the HTML.

page1.php:

<script type="text/javascript">
    var globalArray = <?php echo json_encode($freres); ?>;

    jQuery(function($) {
        liste(); // Ajax call to get HTML for the data in "globalArray"
    });
</script>

AJAX call:

function liste() {
    $.ajax({
        data : {
            array : globalArray,
            dataName : 'someName',
            file : 'templates/t_item_file'
        },
        dataType : 'html',
        success : function(data, textStatus, jqXHR) {
                var table = $('table');
                var rows = $('<table>' + data + '</table>').find('tr');

                rows.each(function(i, e) { // insert with fade-in animations
                    var row = $(e);

                    row.hide();
                    table.append(row);
                    row.delay(i * 15).fadeIn(250);
                });
        },
        type : 'GET',
        url : config.site + 'ajax/view' // configured in header
    });
}

Somewhere in t_header.php:

<script type="text/javascript">
    var config = {
        base : "<?php echo base_url(); ?>",
        site : "<?php echo site_url(); ?>"
    };
</script>

The config route that redirects to ajax/view/...

$route['ajax/(:any)'] = 'c_ajax/$1';

The method of the controller c_ajax that handles the AJAX call:

public function view() {
        $file = $this->input->get('file');
        $array = $this->input->get('array');
        $dataName = $this->input->get('dataName');

        foreach ($array as $vars) {
            $data[$dataName] = $vars;

            $this->load->view($file, $data);
        }
    }

When I do this using EasyPHP on localhost, everything works fine, and I receive the HTML as expected, something like :

<TR>
    <TD>...</TD>
    //...
</TR>
<TR>
//...

And then I insert it into a table. But, when I try to do it on my website in FireBug, I can see that the AJAX response is not 200, but 302 Moved Temporarily.

Can anyone help me to figure out what to do to get it to work, because I spend almost the last four days learning jQuery and AJAX, and it doesn't work (online only).

  • 写回答

2条回答 默认 最新

  • douyu4535 2013-06-24 21:19
    关注

    Problem solved. I load the HTML data in PHP and pass it to JavaScript, and then use jQuery to animate DOM Elements.

    Previously, I didn't pass HTML but raw data in a PHP array, and then tried to get the HTML for all the elements of thie array via Ajax (HTML for all elements in only one call). I think there was too many parameters in the request and that's probably was caused the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题