Magento通过控制器获取整个页面,而不仅仅是变量值


magento getting the whole page instead of just variables value via controller

我希望当通过控制器发出ajax请求时,控制器从efund_2.phtml返回变量的值,但它返回发起方的(efund.phtml)整个html。

 /**
    * Called using AJAX POST method.
    */
    public function getEfundAction()
    {
        // Get post data.
        $PostData = $this->getRequest()->getPost();
        $Param1 = $PostData['param1'];
        $Param2 = $PostData['param2'];
        $ModelLayout = new Pteb_System_Model_Layout_Backend();
        $ModelLayout->LoadLayout();
        $Block = $ModelLayout->SetContentBlock( 'efund-block', 'Pteb_System_Block_Cms' );
        $Block->setTemplate('adminpteb/efund/efund_2.phtml');
        // Passing parameters to template file.
        $Block->setData( 'MyPostData1', $Param1 );
        $Block->setData( 'MyPostData2', $Param2 );
        $ModelLayout->ShowLayout();//this getting the whole page html. But I just need the value returned by the variables in the page(efund_2.phtml).
    }

efund.phtml 页面中的 PHP 函数。

function GetEFund()
{
    var url='http://emall.3pteb.my/adminpteb/efund/getEfund';
    var DivResult=jQuery('#DivResult');
        var data = {
              "action": "test"
            };
            $.ajax({
              type:'GET',
              url:url,
              dataType: "html",
              data: data,
              success: function(data) {
                alert(data);
                DivResult.append(data);
              }
            });
}

而上面的函数是由:

$('a').bind('click', function(){
        GetEFund();
});

我收到此错误:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

在网络标签页(Chrome 控制台)中,我看到调用了 函数 getEfund().但是,当单击它时,它会显示整个 efund.phtml页面的html,实际上这就是返回的内容 控制器。如何使 efund_2.phtml 返回其变量?

似乎您需要在控制器中设置 JSON 标头:

        // Set our return json.
        $sJson = json_encode( $aDataArray, 1 );
        // Send it back to our ajax call.
        $this->getResponse()->setHeader( 'Content-type', 'application/json' );
        $this->getResponse()->setBody( $sJson );