Magento的范围内使用基本AJAX调用 [英] Using Basic AJAX calls within Magento

查看:170
本文介绍了Magento的范围内使用基本AJAX调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个模块开展Magento的一个简单的Ajax调用,但我无法得到它的工作迄今 - 我觉得我缺少的一个重要组成部分的地方。这是我目前拥有的文件:

Creare / Groupedajax /控制器/ AjaxController.php:

 类Creare_Groupedajax_AjaxController扩展Mage_Core_Controller_Front_Action {

    公共职能的indexAction(){
        $这个 - > loadLayout();
        $这个 - > renderLayout();
    }
}
 

Creare / Groupedajax的/ etc / config.xml文件:

 < XML版本=1.0&GT?;
<配置>
  <模块>
    < Creare_Groupedajax>
      <版> 0.1.0< /版本>
    < / Creare_Groupedajax>
  < /模块>
  <前端>
    <路由器>
      < groupedajax>
        <使用>标准< /使用>
        <的args>
          <模块> Creare_Groupedajax< /模块>
          < frontName> groupedajax< / frontName>
        < / ARGS>
      < / groupedajax>
    < /路由器>
    <布局>
      <更新>
        < groupedajax>
          <文件> groupedajax.xml< /文件>
        < / groupedajax>
      < /更新>
    < /布局>
  < /前端>
< /配置>
 

我的AJAX调用:

  $ j.post(groupedajax / AJAX /指数,{大小:$ J(下此).VAL()},功能(数据){
        附加$ J('#结果)HTML(数据)。
    });
 

布局/ groupedajax.xml:

 < XML版本=1.0&GT?;
<布局版本=1.0>
  < groupedajax_ajax_index>
    <块类型=groupedajax / groupedajaxNAME =根输出=toHtml模板=groupedajax / groupedajax.phtml/>
  < / groupedajax_ajax_index>
< /布局>
 

我.phtml作为文件只具有目前在这测试。我只需要我的结果div来返回测试的价值。我只是想知道,如果所有的位都在地方这个工作?

这是我按照教程:<一href="http://www.atwix.com/magento/ajax-requests-in-magento/">http://www.atwix.com/magento/ajax-requests-in-magento/

========================解决=================== =====

我只是需要一个斜杠,在我的网址的开头:

  $ j.ajax({
        网址:/ groupedajax / AJAX /指数,
        键入:POST,
        数据:大小=+ $ J(下此).VAL()
        成功:功能(数据){
        附加$ J('#结果)HTML(数据)。
        }
    });
 

解决方案

如果您的JavaScript是从.phtml作为模板文件被输出,那么你可以使用<一个href="http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/geturl_function_parameters">a方便的功能,以使URL完全限定其随后将进行最安全的方式。

  $ j.ajax({
    网址:&LT; PHP的echo $这个 - &GT;的getURL(groupedajax / AJAX /索引)&GT;?,
    键入:POST,
    数据:大小=+ $ J(下此).VAL()
    成功:功能(数据){
    附加$ J('#结果)HTML(数据)。
    }
});
 

I'm writing a module to carry out a simple Ajax call in Magento, but I'm unable to get it work thus far - I feel like I'm missing a vital component somewhere. These are the files I currently have:

Creare/Groupedajax/controllers/AjaxController.php:

class Creare_Groupedajax_AjaxController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->renderLayout();
    }
}

Creare/Groupedajax/etc/config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <Creare_Groupedajax>
      <version>0.1.0</version>
    </Creare_Groupedajax>
  </modules>
  <frontend>
    <routers>
      <groupedajax>
        <use>standard</use>
        <args>
          <module>Creare_Groupedajax</module>
          <frontName>groupedajax</frontName>
        </args>
      </groupedajax>
    </routers>
    <layout>
      <updates>
        <groupedajax>
          <file>groupedajax.xml</file>
        </groupedajax>
      </updates>
    </layout>
  </frontend>
</config>

My Ajax Call:

$j.post("groupedajax/ajax/index", { size: $j(this).val()}, function(data) {
        $j('#results').html(data);
    });

layout/groupedajax.xml:

<?xml version="1.0"?>
<layout version="1.0">
  <groupedajax_ajax_index>
    <block type="groupedajax/groupedajax" name="root" output="toHtml" template="groupedajax/groupedajax.phtml" />
  </groupedajax_ajax_index>
</layout>

My .phtml file simply has 'test' in it at the moment. I just need my results div to return the 'test' value. I just want to know if all the bits are in place for this to work?

This is the tutorial I have followed: http://www.atwix.com/magento/ajax-requests-in-magento/

======================== SOLVED ========================

I just needed a forward slash at the beginning of my url:

$j.ajax({
        url: "/groupedajax/ajax/index",
        type: "POST",
        data: "size="+$j(this).val(),
        success: function(data) {
        $j('#results').html(data);
        }
    });

解决方案

If your javascript is being output from a .phtml template file then you can use a convenience function to make the URL fully-qualified which will then be the safest way to proceed.

$j.ajax({
    url: "<?php echo $this->getUrl('groupedajax/ajax/index') ?>",
    type: "POST",
    data: "size="+$j(this).val(),
    success: function(data) {
    $j('#results').html(data);
    }
});

这篇关于Magento的范围内使用基本AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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