Search code examples
jqueryajaxjoomla

Ajax in Joomla for a self developed module


I´m trying to use com_ajax for a module I´m creating.

But in my browser console, I get the following error:

"Method getAjax does not exist."

According to the com_ajax All methods must end in Ajax

therefore my code below in the jQuery request, the url &method=get should refer to the function called getAjax() in my helper file

My jQuery request looks like this:

jQuery(document).ready(function(){
    jQuery('.trash').click(function(){
        var del_id= jQuery(this).attr('id');
        var $ele = jQuery(this).parent();
        jQuery.ajax({
            type:'POST',
            url:'index.php?option=com_ajax&module=usernotes&method=getFilterData&format=json',
            data:{del_id:del_id},
            success: function(data){
                console.log(data);
                /*if(data=="YES"){
                    $ele.fadeOut().remove();
                }else{
                    alert("Fejl: kan ikke slettes")
                }*/
            }
        })
    })
});

and this is what I got in my helper file:

class modUserNoteHelper
{
    public static function getFilterDataAjax() {
        $output = $_POST['del_id'];
        echo $output;
        return $output;
    }
}




I´ve also tried like this, which gives me a POST 404 error in the console

jQuery(document).ready(function(){
    jQuery('.trash').click(function(){
        var del_id= jQuery(this).attr('id');
        request = {
            'option' : 'com_ajax',
            'module' : 'usernotes',
            'method' : 'getFilterData',
            'data'   :  del_id,
            'format' : 'raw'
        };
        jQuery.ajax({            
            data   : request,
            type   : 'post',
            success: function( response ) {
                console.log(response);
            }
        });
    })
});

Solution

  • Got it to work! Not sure what made it work, as I´ve tried a really lot and lot count of what did the job..

    Here's the working code: jQuery request

    jQuery(document).ready(function(){
        jQuery('.trash').click(function(){
            var del_id= jQuery(this).attr('id');
            var toDelete = jQuery(this).parent().parent();
            console.log(del_id);
            jQuery.ajax({            
                url: "index.php?option=com_ajax&module=usernotes&format=json&Id="+del_id,
                success: function( response ) {
                    console.log(response);
                    if(response){
                        toDelete.slideUp("normal", function() { jQuery(this).remove(); } );
                    }else{
                        alert("can't delete the row");
                    }
                }
            });
        })
    });
    

    my mod_usernotes.php

    defined('_JEXEC') or die;
    $user = JFactory::getUser();
    $view = JFactory::getApplication()->input->get('view');
    $articleId = JFactory::getApplication()->input->getInt('id');
    
    
    if (!$user->guest && $view == "article") {
    
        require_once dirname(__FILE__) . '/helper.php';
        $helper = new modUserNotesHelper();
        require JModuleHelper::getLayoutPath('mod_usernotes');
    
        if (isset($_POST))
        {
            $post = $app->input->post->get('NoteForm', array(), 'array');
            $helper->CheckUserNote($post);
        }
        $helper->GetUserNotes();
    
    }
    

    My helper

    public static function getAjax()
        {
            $input = JFactory::getApplication()->input;
            $idToDelete  = $input->get("Id"); 
    
            $db = JFactory::getDbo();
    
            $query = $db->getQuery(true);
    
            // delete all custom keys for user 1001.
            $conditions = array(
            $db->quoteName('id') . ' = ' . $idToDelete
            );
    
            $query->delete($db->quoteName('#__user_notes'));
            $query->where($conditions);
    
            $db->setQuery($query);
    
            $result = $db->execute();
    
            return($result);
        }