php – 如何在我自己的插件中使用Joomla Ajax接口
作者:互联网
我正在为Joomla 3开发一个自定义插件.我正在尝试对我的插件进行ajax调用.我已经调查了Joomla Ajax Interface,并按照描述的内容进行了操作.但是当我进行调用时,即使我正在回显一个值,json响应也是空的.
这是我的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请求:
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);
}
});
当我收到响应时,数据变量包含一个空数组.
我究竟做错了什么?谢谢.
解决方法:
以下是触发ajax调用的代码 –
JPluginHelper::importPlugin('ajax');
$plugin = ucfirst($input->get('plugin'));
$dispatcher = JEventDispatcher::getInstance();
try
{
$results = $dispatcher->trigger('onAjax' . $plugin);
}
catch (Exception $e)
{
$results = $e;
}
第一行说插件应该是ajax类型,并且在代码中应该是内容类型.
根据文档,方法和类名约定也不正确 –
The plugin class name following the plgAjax[Name] convention.
The plugin function name following the onAjax[Name] convention.
因此需要首先改变它 –
<?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>
标签:joomla3-0,php,ajax,joomla 来源: https://codeday.me/bug/20190824/1711877.html