简体   繁体   中英

CodeIgniter/AJAX : AJAX does not load partial view

My ajax function only works when i try to load a particular view (createClass.php) but it doesn't work for other views (classAction.php/etc.)

script:

//--------------this works---------------
$(function(){
      $(".classloader").click(function(){
        //alert("clicked");

        $("#functions").load("createClass");
       //$("#functions").load("classActions"); this doesn't work for some reason
      });
    });
   //-----------this doesn't----------- 
    $(function(){
      $(".classAction").click(function(){
        //alert("clicked");
       $("#functions").load("classActions");
      //-- $("#functions").load("createClass"); but this works for some reason 
      });
    });

view:

createClass (partial view):

<div class="jumbotron col-md-12">
      createClass <!-- h1 -->
</div>

classAction (partial view):

<div class="jumbotron col-md-12">
      classAction <!-- h1 -->         
</div>

home (main view):

<button type="button" class="btn btn-success btn-block classAction">Class Action</button>

<button type="button" class="btn btn-default btn-lg btn-block classloader" >Create Class</button>

<div class="col-md-offset-1 col-md-8">

        <div class="jumbotron col-md-12" id="functions">
           Partial views should be loaded here

        </div>
    </div>

您是否尝试过base_url

  $("#functions").load("<?php echo base_url('createClass');?>");

Try using the CI load view function:

$("#functions").load("<?php $this->load->view('classAction', TRUE); ?>");

With "true" as second parameter in order to return the view markup. If doesn't work, try adding the file type (classAction.html).

I would recommend checking the console of your firebug (if you are using it). You can also view it on the console of Chrome's developer tools. Try firing up your code and check the console. That's how I fix my problems with scripts that has correct syntax but doesn't do anything

I finally solved it you guys, and to be honest, I fell kind of silly, it turns out that the load function was calling another function in the controller, i thought that it was loading the view directly, so i just created a "classActions" function in the controller and loaded the view from there and it worked fine. Thanks for all your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM