php 在 Magento 中使用基本的 AJAX 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8835146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:36:52  来源:igfitidea点击:

Using Basic AJAX calls within Magento

jqueryajaxmagentopostphp

提问by Adam Moss

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:

我正在编写一个模块来在 Magento 中执行一个简单的 Ajax 调用,但到目前为止我无法让它工作 - 我觉得我在某处缺少一个重要的组件。这些是我目前拥有的文件:

Creare/Groupedajax/controllers/AjaxController.php:

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:

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:

我的 Ajax 调用:

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

layout/groupedajax.xml:

布局/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?

我的 .phtml 文件目前只有“测试”。我只需要我的结果 div 来返回“测试”值。我只是想知道是否所有的位都到位以使其正常工作?

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

这是我遵循的教程: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);
        }
    });

采纳答案by clockworkgeek

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

如果您的 javascript 是从 .phtml 模板文件中输出的,那么您可以使用一个方便的函数来使 URL 完全限定,这将是最安全的处理方式。

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