简体   繁体   中英

Javascript Http Get Request error : NS_ERROR_FAILURE

I'm writting a website (it's my very first without java..) and what is done is simple. I call a javascript function "changeVideo()" that make a request to a php page "GetAVideo.php" which returns the url to a randomly choosen video (choosen between videos files on my server).

Last night, i could watch videos without problem but today, when i load my page, the video is not loaded because the GET request fails : "NS_ERROR_FAILURE: Failure xhr_object.send();"

I can't understand why, here are my sources : 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;  
    }
?>

Thank you !

Answer : I had to use a relative url instead of an absolute one. Firefox recognized it as a cross-site scripting.

I changed : var request = " http://ogdabou.com/php/GetAVideo.php ";

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


THEN : The VideoJS player said : "the content type http text/plain is not supported"

I fixed this by adding "AddType video/webm .webm" to the .htaccess file on the root folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM