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条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)