我成功创建了一个自定义管理模块,并在菜单中成功加载并成功呈现了phtml文件 .
然后我尝试在单击按钮时使用angularJS在相同的管理模块中通过ajax加载一些虚拟数据,但是ajax调用返回magento admin的仪表板的html内容 .
我跟着这个link
以下是控制器的代码
<?php
namespace DVendor\DemoModule\Controller\Adminhtml\DemoPart;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;
class Index extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
protected $jsonPageFactory;
public function __construct(Context $context,PageFactory $resultPageFactory,JsonFactory $jsonPageFactory){
$this->resultPageFactory = $resultPageFactory;
$this->jsonPageFactory = $jsonPageFactory;
parent::__construct($context);
}
public function execute()
{
if($this->getRequest()->isAjax()){
$result = $this->jsonPageFactory->create();
$test=array(
'Firstname' => 'What is your firstname',
'Email' => 'What is your emailId',
'Lastname' => 'What is your lastname',
'Country' => 'Your Country'
);
return $result->setData($test);
}
else{
return $resultPage = $this->resultPageFactory->create();
}
}
}
?>
以下是phtml文件中使用的JS代码
$http({
method:'POST',
url:'<?php echo $this->getUrl('demomodule/demopart'); ?>',
//data:$.param({'limit':$scope.definedlimits.value}),
headers:{'Content-Type':'application/x-www-form-urlencoded'}
}).then(function(response) {
var data = response.data;
console.log(data);
},function(response){
console.log(response.data);
});