gpt4 book ai didi

javascript - magento 通过 Controller 获取整个页面而不仅仅是变量值

转载 作者:行者123 更新时间:2023-11-27 23:15:41 25 4
gpt4 key购买 nike

当通过 Controller 发出ajax请求时,我希望 Controller 从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/.

In the network tab (Chrome console), I see the call is made to the function getEfund(). However when clicked on it, it shows the entire html of efund.phtml page , in fact that was what returned by the controller. How can I make efund_2.phtml return its variables?

最佳答案

似乎您需要在 Controller 中设置 JSON header :

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

关于javascript - magento 通过 Controller 获取整个页面而不仅仅是变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35850615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com