venky
venky

Reputation: 83

jquery Ajax call Codeigniter Controller

In my View page jquery Ajax call like this

onclick: function() {
                        $.ajax({
                            type:'POST',
                            url:"<?PHP echo base_url('trand/report/checking'); ?>",
                            data: {nm:'vnky'},
                            success: function(){
                                  alert("success");
                              },
                              error: function(){
                                alert("error");
                              }
                        });
                        chart2.exportChart({
                            type: 'image/png', 
                            filename: dynmicfilename
                        });

                    }

exportchart function works perfectly .Inside ajax call also working alerts nice, but url is not executed, by using firebug when clicking the url in new tab , then it works fine.

How can I execute url in ajax call. can you help on this ?

Upvotes: 5

Views: 33819

Answers (2)

kay
kay

Reputation: 337

function generate_dateOfBirth()
{

    $data = $this->input->post('data', TRUE);

    if (!empty($data))
    {

        // Check if the ID Number supplied is not less than 13 characters
        if ( strlen(trim($data)) == 13)
        {
            $year = substr($data, 0, 2);
            $month = substr($data, 7, 2);
            $day = substr($data, 4, 2);

            $dateOfBirth = $year .'/'. $month .'/'. $day ;

            echo $dateOfBirth;
        }
        else
        {
            echo 'You have entered Invalid ID number above';
        }

    }
} 

$('#id_number').on('change', function()
        {
            var dob = $(this).val(); 

            $.ajax({

                    url: '/generate_date',
                    method: 'POST',
                    data: 'data=' + dob,
                    cache: false,
                    type: 'json',
                    success:function(data){
                      //update Increase month
                      $('#dob').val(data);
                    }

            }); //End of ajax call

        }); $route['generate_data'] = 'Controller Name/function in the controller doesn"t contain html';

Upvotes: 0

Minh Quy
Minh Quy

Reputation: 665

Here's my code. My controller return json data

$('.edit').click(function() {
    $.ajax({
        url: '<?php echo site_url('your_controller'); ?>',
        type: 'POST',
        data: {
            key: value
        },
        dataType: 'json',
        success: function(data) {
            console.log(data);
        }
    });
});

Inside Ajax call, which alert is shown success or error? I think your JS code is correct. You should check your controller. If you open it in browser and it work fine. You should check csrf_protection config is TRUE or FALSE

Upvotes: 9

Related Questions