我正在使用JSON Ajax函数调用一个简短的PHP脚本。以下是PHP脚本。
<?php
// returns JSON
include_once("../sqlWorker.php");
include_once("../bkSupplyContext.php");
$helper = new bkSupplyContext();
$dbContext = new sqlWorker();
$resources = $dbContext->getFullSortedEntityArray("popeyesResources", "subCatID");
$returnResources = array();
foreach($resources as $resource) {
$subCatName = $helper->getSubcategoryName($resource->subCatID);
$newResource = $resource;
$newResource->subCatID = $subCatName;
array_push($returnResources, $newResource);
}
echo json_encode($returnResources);
?>
我需要脚本做的是从数据库中获取项目(将结果拉入自定义类对象数组),更改属性(subCatID),然后将其作为JSON传递给AJAX函数但是,变化没有保持。我仍然得到数字subCatID值而不是名称。
我尝试过多次修复。最初,我没有创建一个新的数组,我只是迭代$资源,编辑$ resource并转移到下一个条目。然而,创建一个新的数组和新对象没有任何区别。我仍然得到数值。另外,我尝试使用“&amp;”在循环变量($ resource)之前。我甚至尝试只为属性分配一个常量字符串值(“Hello”),但仍然以数值结束。
我非常困惑,非常感谢任何帮助。
ajax脚本(data [i] .subCatID仍然给我一个数值)。
$.ajax({
data : "json",
url : "/new/includes/ajaxScripts/loadPopeyes.php",
success : function(data) {
var currentSubCat = "";
var popeyesResources = "<form method='post' enctype='multipart/form-data' action='editResources.php'><input type='hidden' name='tableName' value='popeyesResources' /><table><tr><th>Name</th><th>View</th><th>File</th><th>Save Changes</th><th>Delete</th></tr>";
data = JSON.parse(data);
for (var i = 0; i < data.length; i++) {
if (currentSubCat != ""
&& data[i].subCatID != currentSubCat) {
popeyesResources += "<tr class='divider'><td colspan='5'>" + currentSubCat + "</td></tr>";
}
popeyesResources += "<tr>";
popeyesResources += "<td><input name='resourceName"
+ data[i].id + "' type='text' value='"
+ data[i].name + "' /></td>";
popeyesResources += "<td><a target='_blank' href='files/"
+ data[i].link + "' >View</a>";
popeyesResources += "<td><input name='resourceFile"
+ data[i].id + "' type='file' /></td>";
popeyesResources += "<td><button class='saveButton' name='saveButton' value='"
+ data[i].id + "'>Save</button></td>";
popeyesResources += "<td><input type='button' id='deleteButton"
+ data[i].id
+ "' onclick='deleteClick(this)' data-resource-table='popeyesResources' data-resource-id='"
+ data[i].id
+ "' class='deleteButton' value='Delete' /></td>";
popeyesResources += "</tr>";
currentSubCat = data[i].subCatID;
}
popeyesResources += "</table></form>";
$("#popeyesResourceContainer").append(popeyesResources);
}
});
谢谢!