Javascript Ajax调用PHP类返回数组,数组索引返回未定义
我正在尝试向selecthtml元素添加数组值。我的问题如下:Javascript Ajax调用PHP类返回数组,数组索引返回未定义,javascript,php,ajax,Javascript,Php,Ajax,我正在尝试向selecthtml元素添加数组值。我的问题如下: array(1) { ["categories"]=> array(12) { ["4ffc3111a41f46bf58590aa0bb200630"]=> array(5) { ["name"]=> string(5) "Hairs" ["id"]=> string(8) "20074652" ["active"]=>
array(1) {
["categories"]=>
array(12) {
["4ffc3111a41f46bf58590aa0bb200630"]=>
array(5) {
["name"]=>
string(5) "Hairs"
["id"]=>
string(8) "20074652"
["active"]=>
string(4) "true"
["displayName"]=>
string(5) "Hairs"
["previewUrl"]=>
string(13) "stud/hair.swf"
}
["7f579c66d0a7d4d007a15eb17682bd8e"]=>
array(5) {
["name"]=>
string(6) "Shirts"
["id"]=>
string(8) "20074653"
["active"]=>
string(4) "true"
["displayName"]=>
string(6) "Shirts"
["previewUrl"]=>
string(13) "stud/tops.swf"
}
["29d7993b5db7388915fe7067517999db"]=>
array(5) {
["name"]=>
string(7) "Jackets"
["id"]=>
string(8) "20074654"
["active"]=>
string(4) "true"
["displayName"]=>
string(7) "Jackets"
["previewUrl"]=>
string(16) "stud/jackets.swf"
}
["a274880556d8853de27876a5cffd0fb9"]=>
array(5) {
["name"]=>
string(7) "Bottoms"
["id"]=>
string(8) "20074655"
["active"]=>
string(4) "true"
["displayName"]=>
string(7) "Bottoms"
["previewUrl"]=>
string(16) "stud/bottoms.swf"
}
["a60ba1a7a56c9e00252baebe89d62c12"]=>
array(5) {
["name"]=>
string(5) "Shoes"
["id"]=>
string(8) "20074656"
["active"]=>
string(4) "true"
["displayName"]=>
string(5) "Shoes"
["previewUrl"]=>
string(14) "stud/shoes.swf"
}
["643fdaf1fef54e8def751f54b706cd60"]=>
array(5) {
["name"]=>
string(6) "Gloves"
["id"]=>
string(8) "20074657"
["active"]=>
string(4) "true"
["displayName"]=>
string(6) "Gloves"
["previewUrl"]=>
string(15) "stud/gloves.swf"
}
["044521e5145fe9bef15063b9594ee586"]=>
array(5) {
["name"]=>
string(5) "Belts"
["id"]=>
string(8) "20074658"
["active"]=>
string(4) "true"
["displayName"]=>
string(5) "Belts"
["previewUrl"]=>
string(14) "stud/belts.swf"
}
["8111b0006bc6d13725a81813e5a98a09"]=>
array(5) {
["name"]=>
string(7) "Scarves"
["id"]=>
string(8) "20074659"
["active"]=>
string(4) "true"
["displayName"]=>
string(7) "Scarves"
["previewUrl"]=>
string(16) "stud/scarves.swf"
}
["1765c2917753cf1c0e6994b3ef362639"]=>
array(5) {
["name"]=>
string(4) "Hats"
["id"]=>
string(8) "20074660"
["active"]=>
string(4) "true"
["displayName"]=>
string(4) "Hats"
["previewUrl"]=>
string(13) "stud/hats.swf"
}
["9422d6dde5a1c70984984f203ee168df"]=>
array(5) {
["name"]=>
string(4) "Bags"
["id"]=>
string(8) "20074661"
["active"]=>
string(4) "true"
["displayName"]=>
string(4) "Bags"
["previewUrl"]=>
string(13) "stud/bags.swf"
}
["af411d238070f3062264110665b9a4bc"]=>
array(5) {
["name"]=>
string(12) "Miscellanies"
["id"]=>
string(8) "20074662"
["active"]=>
string(4) "true"
["displayName"]=>
string(4) "Misc"
["previewUrl"]=>
string(13) "stud/misc.swf"
}
["398c88178452f433328cf6ed836a47a4"]=>
array(5) {
["name"]=>
string(17) "FacialDecorations"
["id"]=>
string(8) "20074663"
["active"]=>
string(4) "true"
["displayName"]=>
string(5) "Masks"
["previewUrl"]=>
string(14) "stud/masks.swf"
}
}
}
一旦我尝试使用AJAX从PHP类获取数组,响应将返回undefined
,如下面的屏幕截图所示:
我的代码如下所示:
array(1) {
["categories"]=>
array(12) {
["4ffc3111a41f46bf58590aa0bb200630"]=>
array(5) {
["name"]=>
string(5) "Hairs"
["id"]=>
string(8) "20074652"
["active"]=>
string(4) "true"
["displayName"]=>
string(5) "Hairs"
["previewUrl"]=>
string(13) "stud/hair.swf"
}
["7f579c66d0a7d4d007a15eb17682bd8e"]=>
array(5) {
["name"]=>
string(6) "Shirts"
["id"]=>
string(8) "20074653"
["active"]=>
string(4) "true"
["displayName"]=>
string(6) "Shirts"
["previewUrl"]=>
string(13) "stud/tops.swf"
}
["29d7993b5db7388915fe7067517999db"]=>
array(5) {
["name"]=>
string(7) "Jackets"
["id"]=>
string(8) "20074654"
["active"]=>
string(4) "true"
["displayName"]=>
string(7) "Jackets"
["previewUrl"]=>
string(16) "stud/jackets.swf"
}
["a274880556d8853de27876a5cffd0fb9"]=>
array(5) {
["name"]=>
string(7) "Bottoms"
["id"]=>
string(8) "20074655"
["active"]=>
string(4) "true"
["displayName"]=>
string(7) "Bottoms"
["previewUrl"]=>
string(16) "stud/bottoms.swf"
}
["a60ba1a7a56c9e00252baebe89d62c12"]=>
array(5) {
["name"]=>
string(5) "Shoes"
["id"]=>
string(8) "20074656"
["active"]=>
string(4) "true"
["displayName"]=>
string(5) "Shoes"
["previewUrl"]=>
string(14) "stud/shoes.swf"
}
["643fdaf1fef54e8def751f54b706cd60"]=>
array(5) {
["name"]=>
string(6) "Gloves"
["id"]=>
string(8) "20074657"
["active"]=>
string(4) "true"
["displayName"]=>
string(6) "Gloves"
["previewUrl"]=>
string(15) "stud/gloves.swf"
}
["044521e5145fe9bef15063b9594ee586"]=>
array(5) {
["name"]=>
string(5) "Belts"
["id"]=>
string(8) "20074658"
["active"]=>
string(4) "true"
["displayName"]=>
string(5) "Belts"
["previewUrl"]=>
string(14) "stud/belts.swf"
}
["8111b0006bc6d13725a81813e5a98a09"]=>
array(5) {
["name"]=>
string(7) "Scarves"
["id"]=>
string(8) "20074659"
["active"]=>
string(4) "true"
["displayName"]=>
string(7) "Scarves"
["previewUrl"]=>
string(16) "stud/scarves.swf"
}
["1765c2917753cf1c0e6994b3ef362639"]=>
array(5) {
["name"]=>
string(4) "Hats"
["id"]=>
string(8) "20074660"
["active"]=>
string(4) "true"
["displayName"]=>
string(4) "Hats"
["previewUrl"]=>
string(13) "stud/hats.swf"
}
["9422d6dde5a1c70984984f203ee168df"]=>
array(5) {
["name"]=>
string(4) "Bags"
["id"]=>
string(8) "20074661"
["active"]=>
string(4) "true"
["displayName"]=>
string(4) "Bags"
["previewUrl"]=>
string(13) "stud/bags.swf"
}
["af411d238070f3062264110665b9a4bc"]=>
array(5) {
["name"]=>
string(12) "Miscellanies"
["id"]=>
string(8) "20074662"
["active"]=>
string(4) "true"
["displayName"]=>
string(4) "Misc"
["previewUrl"]=>
string(13) "stud/misc.swf"
}
["398c88178452f433328cf6ed836a47a4"]=>
array(5) {
["name"]=>
string(17) "FacialDecorations"
["id"]=>
string(8) "20074663"
["active"]=>
string(4) "true"
["displayName"]=>
string(5) "Masks"
["previewUrl"]=>
string(14) "stud/masks.swf"
}
}
}
Ajax和html:
<div class="col-sm-6">
<label>Gender</label>
<select id="fds_gender" name="fds_gender" data-placeholder="Select a gender..." class="select-icons">
<option value="stud" data-icon="man">Male</option>
<option value="babe" data-icon="woman">Female</option>
</select>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#fds_gender').change(function () {
if ($(this).val() == "stud") {
$.ajax({
type: "POST",
data: {
'username' : '<?php echo $user->username; ?>',
'gender' : 'stud'
},
url: "ajax/fds_categories_ajax.php",
success: function(data){
console.log(data['name']);
}
});
}else if($(this).val() == "babe"){
$.ajax({
type: "POST",
data: {
'username' : '<?php echo $user->username; ?>',
'gender' : 'babe'
},
url: "ajax/fds_categories_ajax.php",
success: function(data){
console.log(data['name']);
}
});
}
});
});
</script>
有人知道为什么console.log始终返回未定义的吗?您正在返回一个
PHP
数组对象,并试图让JavaScript理解它
而是将其转换为JSON数组:
$response = json_encode($fds_categories_response);
然后echo
它(return
不输出任何数据):
然后在AJAX
中,将其转换为JavaScript数组并查看控制台:
$.parseJSON(data);
console.log(data);
要获取类别名称,它将类似于:
var categories = data.categories;
$.each(index, element){
console.log(element.name);
});
您正在返回一个
PHP
array对象,并试图让JavaScript理解它
而是将其转换为JSON数组:
$response = json_encode($fds_categories_response);
然后echo
它(return
不输出任何数据):
然后在AJAX
中,将其转换为JavaScript数组并查看控制台:
$.parseJSON(data);
console.log(data);
要获取类别名称,它将类似于:
var categories = data.categories;
$.each(index, element){
console.log(element.name);
});
如果希望返回某些内容,则无法从PHP函数返回任何值。使用echo函数并在ajax调用函数中读取如果希望返回某些内容,则无法从PHP函数返回任何值。使用echo函数并在ajax调用函数中读取