gpt4 book ai didi

javascript - 在ajax下使用每个循环时出错

转载 作者:行者123 更新时间:2023-11-29 15:31:42 25 4
gpt4 key购买 nike

当我在 Ajax 调用下尝试每个循环时,出现如下错误:

TypeError: invalid 'in' operand e

下面是我的Ajax调用代码

    $.ajax({
type: "POST",
url: "/admin/counselormanagement/centername",
data: 'groupId='+valueSelected,
async: true,
success: function(arrCenter) {
$.each(arrCenter, function( intValue, arrValue ) {
console.log('<option value="' + arrValue['ID'] + '">'+ arrValue['CenterName'] +'</option>');
});
}
});

我从服务器返回的响应是:

Array (
[0] => Array
(
[ID] => 4
[CenterName] => test2
[ParentName] => 2
[Parent] => 3
[GroupName] => test
[Type] => 1
)

[1] => Array
(
[ID] => 8
[CenterName] => test21
[ParentName] => 2
[Parent] => 3
[GroupName] => test
[Type] => 1
)
)

我正在使用 PHP 作为后端,其代码是:

$arrCenterName   = array();
$objCenterMapper = new Application_Model_CentersMapper();
$arrCenter = $objCenterMapper->seekCenters($_POST['groupId']);
print_r($arrCenter[0]);
die();

最佳答案

在 PHP 中使用 json_encode() 返回响应。你的 JS 代码应该是这样的:

PHP:

$arrCenterName   = array();
$objCenterMapper = new Application_Model_CentersMapper();
$arrCenter = $objCenterMapper->seekCenters($_POST['groupId']);
echo json_encode($arrCenter[0]);
die();

JQuery:

$.ajax({
type: "POST",
url: "/admin/counselormanagement/centername",
data: 'groupId='+valueSelected,
dataType: 'json',
async: true,
success: function(arrCenter) {
$.each(arrCenter, function( intValue, arrValue ) {
console.log('<option value="' + arrValue.ID + '">'+ arrValue.CenterName +'</option>');
});
}
});

关于javascript - 在ajax下使用每个循环时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34294368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com