Php Magento 2管理AJAX路由不工作
我试图在Magento 2的管理区域通过AJAX访问URL。我试过很多方法,但每次都是404禁止回复 Firefox的控制台显示: 这是我的模块的代码: 供应商/Module/etc/Module.xmlPhp Magento 2管理AJAX路由不工作,php,ajax,magento2,Php,Ajax,Magento2,我试图在Magento 2的管理区域通过AJAX访问URL。我试过很多方法,但每次都是404禁止回复 Firefox的控制台显示: 这是我的模块的代码: 供应商/Module/etc/Module.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:fr
<?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>
<?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>
供应商/Module/etc/adminhtml/routes.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>
<?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)
});
new Ajax.Request('adminhtml/action/add'{
方法:“post”,
参数:{'order_id':},
onSuccess:函数(响应){
console.log($response);
这个。添加();
}.绑定(此)
});
供应商/Module/Controller/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;
}
}
我确信,路由配置文件中缺少frontName是出现错误404的原因,因此请按照上面的指定将xyz替换为您的frontName
我确信,路由配置文件中缺少frontName是出现错误404的原因,因此请按上述指定将xyz替换为您的frontName我尝试了上述所有方法,但没有一种方法有效或不完整,因此我决定详细回答:
下面是模块最小图
(一)
gpImageTagSaveUrl
是一个globel变量,它将保存您的模块url,您可以在一些js文件中声明它,如下所示
我使用了catalog_product_edit.xml文件,因为我希望它位于产品编辑位置。您可以将它添加到所需的文件中,或者如果需要的话,添加到default.xml中
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="js">
<block class="Magento\Backend\Block\Template" template="Textmimedia_Partpicker::js.phtml"
name="custom_js_backend"/>
</referenceContainer>
</body>
</page>
现在在js.phtml中声明操作url,如下所示:
<script>
require([
"prototype"
], function () {
window.gpImageTagSaveUrl = '<?= /** @noEscape */ $block->getUrl('groupproduct/imagetag/save')?>';
});
</script>
要求([
“原型”
],函数(){
window.gpImageTagSaveUrl='';
});
我尝试了上述所有方法,但没有一种有效或不完整,因此我决定详细回答:
下面是模块最小图
(一)
gpImageTagSaveUrl
是一个globel变量,它将保存您的模块url,您可以在一些js文件中声明它,如下所示
我使用了catalog_product_edit.xml文件,因为我希望它位于产品编辑位置。您可以将它添加到所需的文件中,或者如果需要的话,添加到default.xml中
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="js">
<block class="Magento\Backend\Block\Template" template="Textmimedia_Partpicker::js.phtml"
name="custom_js_backend"/>
</referenceContainer>
</body>
</page>
现在在js.phtml中声明操作url,如下所示:
<script>
require([
"prototype"
], function () {
window.gpImageTagSaveUrl = '<?= /** @noEscape */ $block->getUrl('groupproduct/imagetag/save')?>';
});
</script>
要求([
“原型”
],函数(){
window.gpImageTagSaveUrl='';
});
404禁止?真奇怪。404通常表示找不到。禁止的状态代码应为403。有些东西配置不正确,虽然我不认为是您在这里显示的代码造成的。@Shahid有解决方案吗?404禁止?真奇怪。404通常表示找不到。禁止的状态代码应为403。有些东西配置不正确,尽管我不认为是您在这里显示的代码造成的。@Shahid有解决方案吗?
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="js">
<block class="Magento\Backend\Block\Template" template="Textmimedia_Partpicker::js.phtml"
name="custom_js_backend"/>
</referenceContainer>
</body>
</page>
<script>
require([
"prototype"
], function () {
window.gpImageTagSaveUrl = '<?= /** @noEscape */ $block->getUrl('groupproduct/imagetag/save')?>';
});
</script>