【发布时间】:2017-11-24 18:06:15
【问题描述】:
我正在尝试通过 Magento 2 管理区域中的 AJAX 访问 URL。我尝试了很多东西,但每次响应都是 404 Forbidden。
这是我的模块的代码:
供应商/模块/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
</module>
</config>
供应商/模块/etc/adminhtml/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="adminhtml">
<module name="Vendor_Module" before="Magento_Backend" />
</route>
</router>
</config>
使用原型的 AJAX JS 代码:
new Ajax.Request('<?php /* @escapeNotVerified */ echo $block->getAdminUrl(); ?>adminhtml/action/add', {
method: 'post',
parameters: {'order_id' : <?php /* @escapeNotVerified */ echo $block->getOrderId(); ?>},
onSuccess: function(response) {
console.log($response);
this.add();
}.bind(this)
});
供应商/模块/控制器/Adminhtml/Action/Add.php
<?php
namespace Vendor\Module\Controller\Adminhtml\Action;
class Add extends \Magento\Backend\App\Action
{
protected $_context;
protected $_pageFactory;
protected $_jsonEncoder;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Json\EncoderInterface $encoder,
\Magento\Framework\View\Result\PageFactory $pageFactory
) {
$this->_context = $context;
$this->_pageFactory = $pageFactory;
$this->_jsonEncoder = $encoder;
parent::__construct($context);
}
public function execute()
{
$response = array('status' => 'success');
$this->getResponse()->representJson($this->_jsonEncoder->encode($response));
return;
}
}
请告诉我如何通过 AJAX 访问此管理 URL。
【问题讨论】:
-
404 禁止?这很奇怪。 404 通常表示未找到。 Forbidden 的状态码应该是 403。有些东西没有正确配置,虽然我不认为是你在这里显示的代码导致了它。
-
@Shahid 有什么解决办法吗?