我的 Magento 模塊遇到問題。它在 localhost 上運行良好,但是當上傳到服務器時,任何 Ajax 請求都會回傳 404 錯誤。我在命名空間 Emps_MAMIntegration 中創建了一個 Magento 2 模塊。我還創建了一個包含以下內容的routes.xml檔案。etc/frontend/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="standard">
<route frontName="mamform" id="mamform">
<module name="EMPS_MAMIntegration" />
</route>
</router>
</config>
在我的模板檔案中,我向檔案發出 ajax 請求, Controller/Index/Number.php如下所示:
$.ajax({
url: '<?= $block->getUrl('mamform/index/number') ?>',
type: 'POST',
data: {
type: 'model',
make: make
},
success: function(data) {
// handle success
},
error: function(error) {
// error handling
}
});
然后在我的Number.php檔案中:
<?php
namespace Emps\MAMIntegration\Controller\Index;
use Magento\Framework\Controller\Result\JsonFactory;
use EMPS\MAMIntegration\Helper\MamService;
class Number extends \Magento\Framework\App\Action\Action
{
private $mamService;
private $resultJsonFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
MamService $_mamService,
JsonFactory $resultJsonFactory
) {
$this->mamService = $_mamService;
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
}
public function execute()
{
$post = $this->getRequest()->getPostValue();
// var_dump($post);
if (!$post) {
$this->_redirect($this->getRequest()->getServer('HTTP_REFERER'));
return;
}
$resultJson = $this->resultJsonFactory->create();
$type = $post['type'];
/// omitted for brevity
return $resultJson->setData(['models' => $data]);
}
}
在 localhost 上,所有 ajax 請求都按預期作業,并回傳所有資料。但是,當我將模塊上傳到生產環境時,它們會因錯誤 404 而失敗。
uj5u.com熱心網友回復:
Magento 的路由可能區分大小寫,具體取決于服務器配置
您的模塊名稱是EMPS_MAMIntegration但您的命名空間是Emps\MAMIntegration。我敢打賭這是它在某些環境而不是其他環境下作業的原因。
嘗試將您的模塊重命名為Emps_MAMIntegration或向我們展示您的 register.php 檔案。還要確保在生產中啟用模塊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496304.html
