skip to Main Content

I’m working on this ajax request with CodeIgniter 3. What I’m doing is press on checkbox and the event calls the function changVisib(). as you will see in the snippet, everything goes well because alert() is called.

But $.ajax() call is completely ignored. I tried $.post(). but no response.

I have searched the web for similar issues, but mine seems different. That’s why I’m obliged to ask.

            function changVisib(elem, picId) {
                if (elem.type === 'checkbox') {
                    var cva = 0;
                    if (elem.checked) {
                        cva = 1;
                    } else {
                        cva = 0;
                    }
                    alert('ok up to here');
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url()?>admin/gallery_update",
                        data: {id: picId, shown: cva},
                        dataType: "json"
                    });
                }
            }

Other jQuery functions work perfectly well. But the call to ajax, post, get seems not to work.

2

Answers


  1. Chosen as BEST ANSWER

    I just discovered where the issue was. ajax queries were ignored because I was using the slim version of jQuery.

    I have come to learn that jquery-3.5.1.slim.min.js does not include ajax and effects, according to https://jquery.com/download/.


  2. You need to add a success handler so that you know that the ajax request finished

            function changVisib(elem, picId) {
                if (elem.type === 'checkbox') {
                    var cva = 0;
                    if (elem.checked) {
                        cva = 1;
                    } else {
                        cva = 0;
                    }
                    alert('ok up to here');
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url()?>admin/gallery_update",
                        data: {id: picId, shown: cva},
                        dataType: "json",
                        success: function(data){
                             alert( "success" );
                        }
                    });
                }
            }
    

    data – the return data from the ajax request

    Read more about the ajax jquery method here
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for…in_statement

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search