如何在 Joomla 组件中发出 Ajax 请求 [英] How to make an Ajax request in Joomla Component

查看:18
本文介绍了如何在 Joomla 组件中发出 Ajax 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我调用 ajax 请求时得到的屏幕截图:

This a screen shot of what I get when I call my ajax request:

如何只运行任务而不打印整个页面?这是我的 ajax 调用:

How do I run only the task, without printing the whole page? This is my ajax call:

$.ajax
({
type: "POST",
url: "index.php?option=com_similar&task=abc",
    data: {
        id: id,
        name: name,
        similar_id: similar_id,
    },
cache: false,
success: function(html)
{
$("#flash").fadeOut("slow");
$("#content"+similar_id).html(html);
} 
});
});

$(".close").click(function()
{
$("#votebox").slideUp("slow");
});

});

推荐答案

不要选择退出或死亡,Joomla!有很好的方法来处理这些东西.

Don't go with exit or die, Joomla! has it's nice way of dealing with this stuff.

以下答案已在 Joomla 中测试!2.5 &3(适用于 1.5.也可以).

The answers below are tested in Joomla! 2.5 & 3 (for 1.5. may work as well).

一般

您用于任务的URL 应如下所示:

Your URL for the task needs to look like this:

index.php?option=com_similar&task=abc&format=raw

您然后创建将使用视图的控制器,假设是 Abc,它将包含文件 view.raw.html(与普通视图文件相同).

You than create the controller which will use the view, let's say Abc, which will contain the file view.raw.html (identical to a normal view file).

下面是生成原始 HTML 响应的代码:

Below you have the code for generate a raw HTML response:

/controller.php

public function abc() 
{
    // Set view
    JRequest::setVar('view', 'Abc');
    parent::display();
}

/views/abc/view.raw.php

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

jimport('joomla.application.component.view');

class SimilarViewAbc extends JView
{
    function display($tpl = null)
    {
        parent::display($tpl);
    }
}

/views/abc/tmpl/default.php

<?php

echo "Hello World from /views/abc/tmpl/default.php";

注意:如果我必须返回 HTML,这是我会使用的解决方案(它更干净并遵循 Joomla 逻辑).要返回简单的 JSON 数据,请参阅下面如何将所有内容放入控制器中.

如果您向子控制器发出 Ajax 请求,例如:

If you make your Ajax request to a subcontroller, like:

index.php?option=com_similar&controller=abc&format=raw

您的子控制器名称(对于原始视图)需要是 abc.raw.php.

Than your subcontroller name (for the raw view) needs to be abc.raw.php.

这也意味着您将/可能有 2 个名为 Abc 的子控制器.

This means also that you will / may have 2 subcontrollers named Abc.

如果您返回 JSON,则使用 format=jsonabc.json.php 可能是有意义的.在 Joomla 2.5 中.我在使用此选项时遇到了一些问题(不知何故输出已损坏),所以我使用了 raw.

If you return JSON, it may make sense to use format=json and abc.json.php. In Joomla 2.5. I had some issues getting this option to work (somehow the output was corrupted), so I used raw.

如果您需要生成有效的 JSON 响应,请查看文档页面 Generating JSON输出

If you need to generate a valid JSON response, check out the docs page Generating JSON output

// We assume that the whatver you do was a success.
$response = array("success" => true);
// You can also return something like:
$response = array("success" => false, "error"=> "Could not find ...");

// Get the document object.
$document = JFactory::getDocument();

// Set the MIME type for JSON output.
$document->setMimeEncoding('application/json');

// Change the suggested filename.
JResponse::setHeader('Content-Disposition','attachment;filename="result.json"');

echo json_encode($response);

您通常会将此代码放在控制器中(您将调用一个模型,该模型将返回您编码的数据——这是一种非常常见的情况).如果你需要更进一步,你也可以创建一个 JSON 视图(view.json.php),类似于原始示例.

You would generally put this code in the controller (you will call a model which will return the data you encode - a very common scenario). If you need to take it further, you can also create a JSON view (view.json.php), similar with the raw example.

安全

既然 Ajax 请求正在运行,请先不要关闭页面.阅读下文.

Now that the Ajax request is working, don't close the page yet. Read below.

不要忘记检查请求是否伪造.JSession::checkToken() 在这里派上用场.阅读有关如何向表单添加 CSRF 反欺骗

Don't forget to check for request forgeries. JSession::checkToken() come in handy here. Read the documentation on How to add CSRF anti-spoofing to forms

多语言网站

如果您在请求中不发送语言名称,Joomla 可能不会翻译您想要的语言字符串.

It may happen that if you don't send the language name in the request, Joomla won't translate the language strings you want.

考虑以某种方式将 lang 参数附加到您的请求中(例如 &lang=de).

Consider appending somehow the lang param to your request (like &lang=de).

Joomla 3.2 中的新功能! - Joomla!Ajax 接口

Joomla 现在提供了一种轻量级的方式来处理插件或模块中的 Ajax 请求.您可能想使用 Joomla!如果您还没有组件或需要从已有的模块发出请求,请使用 Ajax 接口.

Joomla now provides a lightweight way to handle Ajax request in a plugin or module. You may want to use the Joomla! Ajax Interface if you don't have already a component or if you need to make requests from a module your already have.

这篇关于如何在 Joomla 组件中发出 Ajax 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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