views:

47

answers:

1

Calling the ajax called URL works well without ajax eg. http://localhost/ci/controller/method/param_value. But using ajax it doesnt work. Following are the code segment, i have just started with JQuery+Ajax with CI.

Here is my controller:

<?php

class BaseController extends Controller{

public function BaseController(){
    parent::Controller();
    $this->load->helper('url');
}
function index(){
    $this->load->view('header');
    $this->load->view('body');
    $this->load->view('footer');
}
function ajaxTest($testData=""){
    //$this->load->view('ajaxtest',array('test_data'=>$testData));
    echo 'Got It';
}

} ?>

Here is View

<?php

echo $test_data; ?>

Ajax Call Function

function testAjaxCall(){

$.post(
    base_url+'basecontroller/ajaxtest/test-value',
    function(responseData) {
        $("#test-div").html(responseData);
    }
);

}

Ajax Calling Page(button)

<div id="test-div"></div><input type="button" onclick="testAjaxCall()" name="ajax-test" value="Test Ajax" />

JS base_url declaration

    <script type="text/javascript" src="<?php echo base_url();?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>js/application-ajax-call.js"></script>

<script type="text/javascript">
/* to make ajax call easy */
base_url = '<?php echo base_url();?>';
</script>

Oh! interestingly, its working on IE8 but not FF. I am getting no clue.

A: 

have you defined base_url properly in your JS?

try changing this

base_url+'basecontroller/ajaxtest/test-value',

to

'<?=base_url()?>basecontroller/ajaxtest/test-value',

EDIT: Also I would highly advise you to install Firebug (Firefox addon) so you can debug your Ajax Posts.

EDIT2: CI doesnt allow passing parameters through GET requests. try changing $.get( to $.post(

Iraklis
yes i have added. Now i have added base_url term to the question too.
Sadat
same for post, i have edited the question too
Sadat
please install firebug, enable net console, and post the resulting errors. Are you getting a 404 not found? if so what is the exact url that is posted?
Iraklis
no no error found even no notification
Sadat