Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter AJAX bug returning same page

when i try to make AJAX request with jQuery as a response i get the html of the same page ! here is a live preview (edit not available due to me fixing it )

here are my files Edit : I have made changes to some of the files

main controller :

Class Main extends Controller {

function Main() 
{
      parent::Controller();   
}

function index(){

    $this->load->view('oxila_index');
}}

Oxila_index view ( just the JS rest of the html is in the link above )

 <script type="text/javascript">
        $(document).ready(function(){
            $("#inv").hide();
        });
        $(document).ready(function(){
            $("#submit").click(function(evt){

                $.post("/ajax/process", {
                    url: $("#url").val()
                }, function(response){
                        $("#output").html("");
                    $("#inv").show("slow"); 
                        $("#output").html(response);
                }, "text");
                evt.preventDefault();
            }); 
        });
    </script>

Ajax Controller

Class Ajax extends Controller {

    function process(){

        $data['url'] = $this->input->post('url');
        $this->load->view('test',$data);
        echo "hello world";
    }
}
like image 330
Nikola Sivkov Avatar asked Nov 05 '22 17:11

Nikola Sivkov


1 Answers

I just tried your page, and it work well. FYI, I use google chrome in Linux.

I have a few notes though. First, move the script from <head> to the bottom of the page, above </body>. This is best practice, since loading js code will block concurrent loading of other page element, css and images.

Second, if you not change anything in the server side, use GET instead of POST. To avoid caching in IE, just add another parameter that have random value.

Third, the line $("#output").html(""); is not necessary. You can put the $("#output").html(response); because .html() will replace any existing content inside the container. No need to emptied it.

like image 51
Donny Kurnia Avatar answered Nov 12 '22 10:11

Donny Kurnia