如何使组件的Joomla一个Ajax请求 [英] How to make an Ajax request in Joomla Component

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

问题描述

这一个屏幕截图我所得到的,当我打电话给我的Ajax请求:

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

  $。阿贾克斯
({
键入:POST,
?的index.php选项= com_similar和放大器;任务= ABC:URL,
    数据: {
        ID:身份证,
        名称:名称,
        similar_id:similar_id,
    },
缓存:假的,
成功:函数(HTML)
{
$(#闪)淡出(慢)。
$(#内容+ similar_id)。html的(HTML);
}
});
});

$(亲密)。点击(函数()
{
$(#votebox)效果基本show(慢)。
});

});
 

解决方案

不要去,退出或死亡的Joomla!有它的处理这些东西的很好的方式。

下面的的Joomla测试的答案! 2.5安培; 3(1.5,可能工作也一样)。


常规

您的网址的任务需要是这样的:

的index.php选项= com_similar和放大器;任务= ABC&放大器;格式=原

您不是创建一个将使用的视图,让我们说ABC,其中将包含文件view.raw.html(等同于一个普通视图文件)控制器。

下面你有$ C $下生成一个原始的HTML响应:

/controller.php

 公共职能ABC()
{
    //设置视图
    JRequest :: SETVAR(观看,ABC);
    父母::显示();
}
 

/views/abc/view.raw.php

 < PHP
定义('_ JEXEC)或死亡;

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

类SimilarViewAbc扩展JView中
{
    功能显示($第三方物流= NULL)
    {
        父母::显示($ TPL);
    }
}
 

/views/abc/tmpl/default.php

 < PHP

回声的Hello World从/views/abc/tmpl/default.php;
 

注:这是我会用,如果我不得不返回HTML(这是更清洁和遵循的Joomla逻辑)的解决方案。对于简单的返回JSON数据,请参见下面如何把一切都在控制。


如果你让你的Ajax请求到子控制器,如:

的index.php选项= com_similar和放大器;?控制器= ABC&放大器;格式=原

比你的子控制器名称(原始视图)需要 abc.raw.php

这也意味着你会/可能有2子控制器名为ABC。

如果您返回JSON,它可能是有意义的使用格式= JSON abc.json.php 。在的Joomla 2.5。我有一些问题,让这个选项奏效(莫名其妙的输出被破坏),所以我用原料。


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

  //我们假设whatver你这样做是成功的。
$响应=阵列(成功=>真正的);
//你也可以返回类似:
$响应=阵列(成功=>错误,错误=>中找不到......);

//获取文档对象。
$文件= JFactory :: getDocument();

//设置的MIME类型JSON输出。
$文档 - > setMimeEncoding(应用/ JSON);

//更改建议的文件名。
JResponse :: SetHeader可以(内容处置,附件;文件名=result.json');

回声json_en code($响应);
 

您会通常把这个code控制器(您将调用一个模型,将返回的数据会连接code - 一个很常见的情况)。如果需要采取进一步,你还可以创建一个JSON视图(view.json.php),与原始的例子相似。


安全

现在的Ajax请求正在工作,请不要关闭该页面还没有。阅读下文。

不要忘了检查请求伪造。 JSession :: checkToken()在这里派上用场。阅读有关如何 CSRF反欺骗添加到形式的文档


多点

有可能发生,如果你不发送语言名称的要求是,Joomla不会翻译所需的语言字符串。

考虑追加不知何故郎参数你的要求(如&放大器; LANG =德)。


在新的Joomla 3.2 的 - !的Joomla! Ajax界面

的Joomla现在提供了一个轻量级的方式来处理一个插件或模块Ajax请求。您可能需要使用的Joomla! Ajax界面,如果你不已经一个组成部分,或者如果你需要从一个模块发出请求您已经有了。

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

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

});

解决方案

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

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


General

Your URL for the task needs to look like this:

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

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).

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

Note: This is the solution I would use if I had to return HTML (it's cleaner and follows Joomla logic). For returning simple JSON data, see below how to put everything in the controller.


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

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

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

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

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.


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

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.


Security

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

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


Multilingual sites

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

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


New in Joomla 3.2! - Joomla! Ajax Interface

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天全站免登陆