問題描述
我正在編寫一個模塊來在 Magento 中執(zhí)行一個簡單的 Ajax 調(diào)用,但到目前為止我無法讓它工作 - 我覺得我在某處缺少一個重要的組件.這些是我目前擁有的文件:
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>
我的 Ajax 調(diào)用:
$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>
我的 .phtml 文件目前只有測試".我只需要我的結(jié)果 div 返回測試"值.我只是想知道是否所有的位都到位以使其正常工作?
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?
這是我遵循的教程:http://www.atwix.com/magento/ajax-requests-in-magento/
======================== 已解決 ==========================
我只需要在我的網(wǎng)址開頭加一個正斜杠:
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);
}
});
推薦答案
如果您的 javascript 是從 .phtml 模板文件輸出的,那么您可以使用 一個方便的函數(shù),使 URL 完全合格,這將是最安全的處理方式.
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);
}
});
這篇關(guān)于在 Magento 中使用基本的 AJAX 調(diào)用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!