如何在Magento中定义ajax调用的URL属性?

时间:2016-03-03 07:07:18

标签: php jquery ajax magento

我正在使用Magento进行开发。我需要对同一页面进行ajax调用,以从相关的php函数中获取一些细节。当我在开发人员工具中检查时,在网络选项卡下,调用当前页面,但它显示以下错误:

{ return _array; }

Invalid URL: adminpteb/efund/efund.phtml l是我启动ajax调用的页面,并且要调用的php函数也位于同一页面中。 Magento有自己的方式通过ajax获取php脚本吗?

参考此链接:Using Basic AJAX calls within Magento

我将URl定义为:url:efund.phtm,但仍然是同样的错误。

ajax警报'完成'并且拾取了正确的功能。但是,函数1返回的值未达到ajax。 我的剧本:

/adminpteb/efund/efund.phtml

PHP脚本

$.ajax({
      method: "GET",
      url: "efund.phtml",
      data: { function_to_call: 0, id: cl[3] }
    }).fail(function() {
      alert( "error" );
    }).always(function() {
       alert( "complete" );
    }).done(function( msg ) {
        var obj = jQuery.parseJSON( msg );
        alert( "ID obtained from server is " + obj.id );
      });

1 个答案:

答案 0 :(得分:1)

您可以通过以下代码获取magento中的当前页面URL

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();

将此网址传递给您的ajax电话

$.ajax({
    method: "GET",
    url: '<?php echo path; ?>',
    data: {function_to_call: 0, id: cl[3]}
}).fail(function () {
    alert("error");
}).always(function () {
    alert("complete");
}).done(function (msg) {
    var obj = jQuery.parseJSON(msg);
    alert("ID obtained from server is " + obj.id);
});