4

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 ?

S. M. Shahinul Islam
  • 2,780
  • 4
  • 36
  • 68
venky
  • 73
  • 1
  • 1
  • 9

2 Answers2

9

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

Minh Quy
  • 655
  • 5
  • 21
  • My Controller also fine. when i open in browser it works fine . csrf_protection in config I changed to TRUE now, but same problem url problem . @Minh Quy – venky Nov 20 '14 at 04:34
  • Open this file `application/config/config.php` and find this value `csrf_protection`. If `csrf_protection = TRUE`, you should change it to `FALSE`. – Minh Quy Nov 20 '14 at 04:39
  • When you enable csrf protection, with each POST request, CI will check csrf token within form data. You using Ajax to make POST request and missing this token – Minh Quy Nov 20 '14 at 04:43
  • yeah its default False. i changed to False now. but still i get url problem. ajax function executed but url is not executed – venky Nov 20 '14 at 04:43
  • Oh. So which alert is shown? success or error? Open Firebug and see http code? – Minh Quy Nov 20 '14 at 04:47
  • error alert shows. using firebug url code i open in new tab , it works fine. don't why it happens . my controller function is right. but still url not executed in ajax function . – venky Nov 20 '14 at 04:54
  • You can find way to catch ajax post error here http://stackoverflow.com/questions/2833951/how-to-catch-ajax-query-post-error. I have no ideal :( – Minh Quy Nov 20 '14 at 05:01
0

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';
kay
  • 337
  • 3
  • 7