2

我正在写一个网站(这是我第一次没有 java..),所做的很简单。我调用了一个 javascript 函数“changeVideo()”,该函数向 php 页面“GetAVideo.php”发出请求,该页面将 url 返回到随机选择的视频(在我的服务器上的视频文件之间选择)。

昨晚,我可以毫无问题地观看视频,但今天,当我加载我的页面时,由于 GET 请求失败,视频没有加载:“NS_ERROR_FAILURE: Failure xhr_object.send();”

我不明白为什么,这是我的资料来源:javascript:

function changeVideo() 
{
    console.log("changeVideo path:", path);
    var xhr_object = null;
    if (window.XMLHttpRequest) // Firefox 
        xhr_object = new XMLHttpRequest();
    else if (window.ActiveXObject) // Internet Explorer 
        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    var request = "http://ogdabou.com/php/GetAVideo.php";

    console.log("request: ", request);
    xhr_object.open("GET", request, false);
    xhr_object.send();
    console.log("response: ", xhr_object.responseText);
    videoPlayer.src(xhr_object.responseText);
    videoPlayer.currentTime(0);
    videoPlayer.play();
    document.getElementById("videoTitle").innerHTML = xhr_object.responseText;
    return false;
};

php:

<?php
    $dirname = '../videos';

    $videoList = array();

    $dir = opendir($dirname);
    if (count($_GET) > 0)
    {
        $folders=explode(";", $_GET['folders']);
        foreach ($folders as $videoFolder) {
            $fullPath = $dirname."/".$videoFolder;
            echo "Visiting $fullpath";
            $videoList = fillVideoList($fullPath, $videoList);
        }
    }
    else
    {
        $videoList = fillVideoList($dirname, $videoList);
    }
    //use join to get the paths.

    closedir($dir);
    $choosenOne = $videoList[rand(0, count($videoList) - 1)];
    $choosenOne = str_replace("../videos/","", $choosenOne);
    echo "http://ogdabou.com/videos/".$choosenOne;
?>

<?php
    // Fill the videoList with the given folder. Also visit subdirectories.

    function fillVideoList($folder, $videoList)
    {

        $path = $folder;
        $folder = opendir($folder);
        while($file = readdir($folder)) { 
                if($file != '.' && $file != '..')
                {
                        $fullPath = "$path/$file";
                        if (is_dir($fullPath))
                        {
                                $videoList = fillVideoList($fullPath, $videoList);
                        }
                        else if(pathinfo($fullPath, PATHINFO_EXTENSION) == "webm")
                        {
                                $videoList[] = $fullPath;
                        }
                }    
        }
        return $videoList;  
    }
?>

谢谢 !

4

1 回答 1

1

答:我必须使用相对 url而不是绝对的。Firefox 将其识别为跨站点脚本。

我改变了: var request = " http://ogdabou.com/php/GetAVideo.php ";

到 var request = "/php/GetAVideo.php";


那么: VideoJS 播放器说: “不支持内容类型 http text/plain”

我通过将“AddType video/webm .webm”添加到根文件夹上的.htaccess 文件来解决此问题。

于 2013-09-18T09:15:54.193 回答