How to use Joomla Ajax Interface for my own plugin
Asked Answered
Z

2

5

I'm developing a custom plugin for Joomla 3. I'm trying to make an ajax call to my plugin. I've looked into the Joomla Ajax Interface and followed what was described. But when I make the call, the json response is empty, even if I'm echoing a value.

Here is my PHP code:

class plgContentMyPlugin extends JPlugin
{
    public static function onAjaxSendMail()
    {
        //Get the app
        $app = JFactory::getApplication();

        $data = "test";

        //echo the data
        echo json_encode($data);

        //close the $app
        $app->close();
    }
}

Here is my Ajax request:

jQuery.ajax(
{
    type: "POST",
    url: "index.php?option=com_ajax&plugin=myplugin&method=onAjaxSendMail&format=json",
    success: function(data)
    {
         var response = jQuery.parseJSON(data);
         console.log(response);
    }
});

When I receive the response, the data variable contains an empty array.

What am I doing wrong? Thank you.

Zygoma answered 27/1, 2015 at 12:5 Comment(1)
Hi! As you already know, you can use Joomla's own StackExchange site for asking Joomla-related questions: joomla.stackexchange.com.Sophronia
T
9

Below is the code which triggers ajax call -

JPluginHelper::importPlugin('ajax');
$plugin     = ucfirst($input->get('plugin'));
$dispatcher = JEventDispatcher::getInstance();

try
{
    $results = $dispatcher->trigger('onAjax' . $plugin);
}
catch (Exception $e)
{
    $results = $e;
}   

First line says plugin should be of ajax type and in your code its content type. Also method and class name convention is not correct as per documentation -

The plugin class name following the plgAjax[Name] convention.
The plugin function name following the onAjax[Name] convention.

SO need to change that first it should be -

<?php defined('_JEXEC') or die;

// Import library dependencies
jimport('joomla.plugin.plugin');

class plgAjaxMyplugin extends JPlugin
{

    function onAjaxMyplugin()
    {

        $data = array("test");
        return $data;

    }
}

//jQuery

jQuery.ajax(
    {
        type: "POST",
        url: "index.php?option=com_ajax&plugin=myplugin&format=json",
        success: function(data)
        {
             //var response = jQuery.parseJSON(data);
             console.log(data);
        }
    });

//XML

<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5"
           type="plugin"
           group="ajax"
           method="upgrade">
    <name>Ajax - Myplugin</name>
    <version>0.1</version>
    <creationDate>Jan 28, 2015</creationDate>
    <author>test</author>
    <authorEmail>[email protected]</authorEmail>
    <authorUrl>http://www.test.com</authorUrl>
    <license>GNU General Public License version 2 or later</license>
    <copyright>Copyright (C) 2013 betweenbrain llc. All rights reserved.</copyright>
    <description>Joomla Ajax Plugin</description>

    <files>
        <filename plugin="myplugin">myplugin.php</filename>
    </files>

</extension>
Termite answered 27/1, 2015 at 19:55 Comment(0)
M
1

The important notice:

in your ajax call add group of your plugin :

jQuery.ajax(
{
    type: "POST",
    url: "index.php?option=com_ajax&plugin=myplugin&method=onAjaxSendMail&format=json",
    success: function(data)
    {
         var response = jQuery.parseJSON(data);
         console.log(response);
    }
});

change to :

jQuery.ajax(
{
    type: "POST",
    url: "index.php?option=com_ajax&group=Content&plugin=myplugin&method=onAjaxSendMail&format=json",
    success: function(data)
    {
         var response = jQuery.parseJSON(data);
         console.log(response);
    }
});
Mitchelmitchell answered 13/6, 2019 at 16:10 Comment(1)
If you are able to provide Joomla support, please join us at Joomla Stack Exchange and help us to resolve questions.Karalynn

© 2022 - 2024 — McMap. All rights reserved.