duanliao2310 2017-10-09 12:54 采纳率: 100%
浏览 39
已采纳

将ajax响应写入来自php的div

I am trying to make an incredibly simple ajax call, but it wont work. Here is the php side called by ajax( This returns the correct response )

<?php
$returnValue = '';
$allUploads = array_slice(scandir('./uploads'), 2);

$returnValue .= '<table>';
foreach ($allUploads as $upload) {
    $returnValue .= '<tr><a href ="/uploads/' . $upload . '>' . $upload . '</a></tr>';
}
$returnValue .= '</table>';

echo($returnValue);
?>

And here is the javascript thats letting me down

<script>
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange =
            function ()
            {
                if (this.readyState == 4 && this.status == 200)
                {
                    alert(this.responseText);
                    document.getElementById("filedisplaytable").innerHTML = this.responseText;
                }
            };
    xhttp.open("GET", "listuploads.php", true);
    xhttp.send();
</script>

Worst thing about this is, that the alert statement is outputting exactly what I want to write:

<table>
    <tr>
        <a href ="/uploads/DSC001.jpg>DSC001.jpg</a></tr><tr><a href ="/uploads/DSC002.jpg>DSC002.jpg</a>
    </tr>
    <tr>
        <a href ="/uploads/DSC003.jpg>DSC003.jpg</a>
    </tr>
</table>

But when I write that into the div it writes the a hrefs 1st followed by an empty table...

Any help is greatly appreciated.... struggling to do such simple things really gets me down

  • 写回答

2条回答 默认 最新

  • dpdrtj1075 2017-10-09 12:59
    关注

    Your HTML is invalid.

    An <a> element cannot be a child element of a <tr>.

    Only <td> and <th> are allowed there.

    The problem you are experiencing is likely the result of the browser attempting to recover from your error by moving the <a> elements to somewhere they are allowed (i.e. outside the table).

    Write valid HTML instead. It looks like you don't have a tabular data structure so probably shouldn't be using a table in the first place. A list might be a better bet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。