Javascript Http Get Request错误:NS_ERROR_FAILURE

时间:2013-09-18 08:41:30

标签: javascript php request

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

昨晚,我可以毫无问题地观看视频,但今天,当我加载我的页面时,视频未加载,因为GET请求失败: “NS_ERROR_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;  
    }
?>

谢谢!

1 个答案:

答案 0 :(得分:1)

答案: 我不得不使用相对网址而不是绝对。 Firefox认为它是一个跨站点脚本。

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

要       var request =“/php/GetAVideo.php”;


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

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