开发者

ajax and codeigniter duplicating header

I have a strange problem that I am unable to track down. I am using Codeigniter and Jquery to create an ajax search feature.

Everything is working well and the correct data is been returned from my model but when I load the view from the controller it has the header of the page in the response.

I have tried an empty view to load and still the header of the page is included.

My javascript function is like so

        function filter(id)
        {

            var str = "test";
            var series = {}; //init array

            data['id'] = id;

            var url = "/baseurl/controller/methodName";


            $('#result').load( url, data, function(str){} );

        }

Then from within my controller after i have retrieved the results from the model I simply load the view

$this->load->view("results", $data);

Any help or tips to point me in the right direction would be much appreciated.

As requested below is the response:

</head>
<body>
    <div id="container">
    <div id="wrapper">

        <div id="header">

 开发者_如何学JAVA       </div>
        <!-- end header -->         <!-- Menu -->
        <div id="menu">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About Us</a></li>
                <li><a href="#">Testimonials</a></li>
                <li class="last"><a href="#">Links</a></li>
            </ul>

            <div id="sub-menu">
                <ul>
                    <li><a href="#">Profile</a></li>
                    <li class="last"><a href="#">Privacy</a></li>
                </ul>
            </div>

        </div>
        <!-- End Menu -->

Test Ajax

            </div><!-- end houses -->

As shown above the information within the results div is the content coming from the view but all the content above is extra information. It is the rest of the page above the div I am trying to update

Kind Regards Ben


Are using using compression? If you are try to turn it off from CodeIgniter config.php

EDIT:

Option 1: you could put check from your method if you are receiving an ajax call. try creating this helper:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function is_ajax()
{
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}

?> 

Then try doing this in your method:

if (is_ajax())
{
    // do your thing
}
else
{
    $this->load->view('your_view');
}

This hopefully will solve this...

Option 2.

You could just echo and html string e.g. 'your data' and not load view


well now i feel stupid, The code been shown along with the response were views that were been loaded in the parent class.

My bad.

Thanks for all the help and the is_ajax() function is definitely a help.

Regards Ben

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜