douyan4243 2015-02-07 19:48
浏览 19

无法使用json_encoded php数组的元素填充<div>

This function worked previously (the last time I opened this project over a week ago), but now I can't seem to get it to work at all and I have no idea how to figure out what's going wrong! First, I'll diagram my file architecture in case my file paths are incorrect and causing my php to not even be called:

~/Sites
    proj1
        htdocs
            index.html
            ajax.php
            scripts
                java_script.js
            styles
                style_sheet.css
        includes
            scrapedImages
                .
                .
                .

Here's the JS function that uses AJAX to call a .php script:

function requestServer() {
    $.getJSON('/Users/aweeeezy/Sites/proj1/htdocs/ajax.php', function(data) {
        $.each(data, function(key, val) {
            var html = "<img src='/Users/aweeeezy/Sites/proj1/includes/scrapedImages/"+val+"' style='display:block;max-width:20px;max-height:20px;width:auto;height:auto' alt=null />"
            $('#puppy-box').prepend(html);
        });
    });
}

The line setting var html was <img src='...'+images[i]+"... when I first opened the project this morning, but I'm not sure why...I think because I was testing the site out, it was faster to only load a fraction of the images and used a for loop to cycle through only the first 10 or so pictures, hence the i index. Anyway, shouldn't it be data[i], or val[i]...or data[val]? I have no idea what's going on here.

Here's the ajax.php file that the JS function is trying to call:

<?php
    $images = scandir('/Users/aweeeezy/Sites/proj1/includes/scrapedImages/');
    /*foreach ($images as $key => $image) {
        if ($image == '.' || $image == '..') {
            unset($images[$key]);
        }
    }*/
    echo json_encode($images);
?>

I commented out the middle part because I wasn't sure if this was causing a complication. I tried putting both echo and print lines in here to see if the script is being called, but even if they are working, I can't really see them because they're being returned as data (I think) to the JS function that calls the script.

How do I even go about debugging this mess and get my girlfriend's valentine's day project back on track!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘