如何为自己的插件使用Joomla Ajax接口 [英] How to use Joomla Ajax Interface for my own plugin

查看:124
本文介绍了如何为自己的插件使用Joomla Ajax接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Joomla 3开发自定义插件.我正在尝试对我的插件进行ajax调用.我研究了 Joomla Ajax接口,并按照描述进行了操作.但是当我打电话时,即使我回显一个值,json响应也为空.

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.

这是我的PHP代码:

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();
    }
}

这是我的Ajax请求:

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.

我做错了什么?谢谢.

推荐答案

下面是触发ajax调用的代码-

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;
}   

第一行说插件应该是ajax类型,并且在您的代码中是内容类型. 同样,根据文档,方法和类名约定也不正确-

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>admin@change.me</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>

这篇关于如何为自己的插件使用Joomla Ajax接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆