AJAX请求下$phrases数组转为字符串的问题修复求助
问题详情
我在通过AJAX请求获取数据并使用$phrases数组进行文本翻译时遇到问题:
- 非AJAX场景下,
var_dump($phrases)显示为正常数组(包含3140个元素的翻译数组) - 但AJAX请求进入
subcategory方法后,var_dump($phrases)显示为字符串"home",导致翻译功能失效
$phrases原本是从控制器index()方法传入subcategory($phrases)的,请求修复该问题。
正常场景下的$phrases示例
array(3140) { ["absolutely"]=> string(23) "बिल्कुल" ["achieve"]=> string(58) "प्राप्त करना" }
控制器代码
class Lispo extends BaseController { public function index() { $commonData = $this->getCommonData(); //<<>>[[ Access the $phrase and $selectedLanguage variable from the common data ]]<<>>// $phrases = $commonData['phrase']; $this->subcategory($phrases); }//other code here public function subcategory($phrases) { if (isset($_POST['csrf_token']) && CSRFHelper::verifyCsrfToken($_POST['csrf_token'])) { require_once 'app/models/category.class.php'; require_once 'app/libs/cryptor.class.php'; $crypt = new Cryptor(); $categoryModel = new Category(); $translator = new TextTranslator($phrases); $id = trim($_POST['dataId']); $results = $categoryModel->getCategoriesWithSubcategories($id); $html = ''; foreach ($results as $subcategory) { // Add condition for status 0 $statusClass = ($subcategory->status == 0) ? 'fa-solid fa-lock' : 'bi bi-chevron-right'; $disableClass = ($subcategory->status == 0) ? 'disable-click' : ''; $subid = $crypt->encryptId($subcategory->catID); $subcategoryName = $translator->translateText($subcategory->category); $html .= '<div class="s_cali s_vrEfW5 ' . $disableClass . '" role="button" data-id="' . $subid . '"><span class="s_evrEOf">' . $subcategoryName . '</span><span class="' . $statusClass . '"></span></div>'; } // Instead of echo json_encode($html); echo json_encode(['success' => true, 'html' => $html]); } } }
AJAX请求代码
$(document).on("click", ".s_zpSpH4", function (e) { e.preventDefault(); var csrf_token = $(".csrf_token").val(); var dataId = $(this).data("id"); var clickedElement = $(this); // Store the reference to the clicked element $.ajax({ type: "POST", url: BASEURL + "lispo/subcategory", data: { csrf_token: csrf_token, dataId: dataId }, dataType: "json", success: function (data) { console.log(data); if (data.success) { clickedElement.hide(); // Use the stored reference to hide the clicked element $('.s_qeV3Mr').html(data.html); } else { console.log(data.error); } }, error: function (error) { // Handle errors if any console.error("AJAX error:", error); console.log(error.responseText); }, }); });
修复方案
问题根源:AJAX请求是直接访问lispo/subcategory独立路由,框架会直接调用subcategory方法,而非通过index方法调用,导致$phrases参数没有传入正确数组,反而被路由系统默认值填充为字符串"home"。
具体修改步骤
- 修改
subcategory方法,让它自行获取$phrases
public function subcategory() { // 直接在方法内获取翻译数组,和index方法逻辑一致 $commonData = $this->getCommonData(); $phrases = $commonData['phrase']; if (isset($_POST['csrf_token']) && CSRFHelper::verifyCsrfToken($_POST['csrf_token'])) { require_once 'app/models/category.class.php'; require_once 'app/libs/cryptor.class.php'; $crypt = new Cryptor(); $categoryModel = new Category(); $translator = new TextTranslator($phrases); $id = trim($_POST['dataId']); $results = $categoryModel->getCategoriesWithSubcategories($id); $html = ''; foreach ($results as $subcategory) { // Add condition for status 0 $statusClass = ($subcategory->status == 0) ? 'fa-solid fa-lock' : 'bi bi-chevron-right'; $disableClass = ($subcategory->status == 0) ? 'disable-click' : ''; $subid = $crypt->encryptId($subcategory->catID); $subcategoryName = $translator->translateText($subcategory->category); $html .= '<div class="s_cali s_vrEfW5 ' . $disableClass . '" role="button" data-id="' . $subid . '"><span class="s_evrEOf">' . $subcategoryName . '</span><span class="' . $statusClass . '"></span></div>'; } echo json_encode(['success' => true, 'html' => $html]); } }
- 修改
index方法中调用subcategory的逻辑
public function index() { $commonData = $this->getCommonData(); $phrases = $commonData['phrase']; // 无需再传递$phrases参数 $this->subcategory(); }
修改后,无论是index方法调用还是AJAX直接请求,subcategory都能正确获取完整的$phrases数组,翻译功能即可正常工作。
内容的提问来源于stack exchange,提问作者Raj
相关产品推荐
相关产品推荐

