Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

jquery - Using Basic AJAX calls within Magento

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);
        }
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...