如何将PHP关联数组传递给$。ajax与JSON

时间:2022-09-26 08:25:52

$.ajax seems to be broken for me:

美元。阿贾克斯似乎被我打破了:

$.ajax({url:'getGalleries.php', datatype:'jsonp',
    success: function(data){
        $('#galleries').html('');
        $.each(data,function(key,value) {
                $('#galleries').append(value);
        });
    },
    complete: function() { loading.hide(); }
});

The php is just passing:

php正在传递:

<?php echo json_encode(array("associative"=>"arrays","are"=>"cool")); ?>

It seems to be fine with another function that uses just regular arrays, but for some reason my jQuery is spitting out a data that is an array of every character in the JSON string when I pass it a json encoded associative array.

对于另一个只使用常规数组的函数来说,这似乎没什么问题,但出于某种原因,当我向JSON编码的关联数组传递时,我的jQuery生成了JSON字符串中每个字符的数组。

The PHP page is grabbing a json list of image galleries then finding the first image in each gallery. I'm making an associative array with the gallery name as the index to then pass back to my html page to show each of my galleries and a sample image.

PHP页面抓取图像库的json列表,然后在每个图库中找到第一个图像。我将创建一个与图库名作为索引的关联数组,然后返回到html页面,显示我的每个图库和一个示例图像。

2 个解决方案

#1


3  

You have two problems. One is that datatype's capitalization is incorrect; it should be dataType. Second, it isn't JSONP as far as I can see - it's JSON. So use 'json' as the dataType.

你有两个问题。一是数据类型的大小写不正确;应该是数据类型。第二,它不是JSONP,就我所见,它是JSON。因此,使用“json”作为数据类型。

#2


0  

I am guessing that you need to capitalize dataType:

我猜你需要大写数据类型:

$.ajax({url:'getGalleries.php', dataType:'jsonp',
success: function(data){
    $('#galleries').html('');
    $.each(data,function(key,value) {
            $('#galleries').append(value);
    });
},
complete: function() { loading.hide(); }
});

#1


3  

You have two problems. One is that datatype's capitalization is incorrect; it should be dataType. Second, it isn't JSONP as far as I can see - it's JSON. So use 'json' as the dataType.

你有两个问题。一是数据类型的大小写不正确;应该是数据类型。第二,它不是JSONP,就我所见,它是JSON。因此,使用“json”作为数据类型。

#2


0  

I am guessing that you need to capitalize dataType:

我猜你需要大写数据类型:

$.ajax({url:'getGalleries.php', dataType:'jsonp',
success: function(data){
    $('#galleries').html('');
    $.each(data,function(key,value) {
            $('#galleries').append(value);
    });
},
complete: function() { loading.hide(); }
});