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.
Below is the code which triggers ajax call -
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 -
SO need to change that first it should be -
//jQuery
//XML
The important notice:
in your ajax call add group of your plugin :
change to :