magento通过控制器

时间:2016-03-07 17:57:11

标签: javascript php jquery ajax magento

我希望控制器在通过控制器发出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控制台)中,我看到调用了   function getEfund()。但是当点击它时,它会显示整个   efund.phtml页面的html,实际上那是由...返回的   控制器。如何让efund_2.phtml返回其变量?

1 个答案:

答案 0 :(得分:1)

似乎您需要在控制器中设置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 );