5

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 ?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

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
Open this file application/config/config.php and find this value csrf_protection. If csrf_protection = TRUE, you should change it to FALSE.
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
yeah its default False. i changed to False now. but still i get url problem. ajax function executed but url is not executed
Oh. So which alert is shown? success or error? Open Firebug and see http code?
|
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';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.