javascript - 使用 Javascript 检查 m3u8 URL 是否损坏

标签 javascript jquery ajax

我使用以下代码来确定 m3u8 URL 是否损坏。我使用两个不同的 URL 进行测试,一个是在线的,一个是离线的。对于这两种情况,我的 javascript 函数不会提醒我文件是否已找到,并且 Firefox 调试不会给我任何错误,但状态变量始终显示 0。有人能告诉我我在这里做错了什么吗?

编辑:

for offline url i get this header response(in httpfox developer tool) :HTTP/1.1 404 Not Found
for online url i get this header response(in httpfox developer tool) :HTTP/1.1 200 OK

代码:

 <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
    function testFunction() {


    //m="http://someothersite.com/offline.m3u8";
    m="http://somesite.com/workingfile.m3u8";

    //now we checking if the file exist

    UrlExists(m, function(status){
     alert('status:'+status);
        if(status === 200){
           // file was found
           alert('file found'+m);

        }
        else if(status === 404){
           // 404 not found
           alert('file not found'+m);


        }
    });

    function UrlExists(url, cb){
        jQuery.ajax({
            url:      url,
            dataType: 'text',
            type:     'GET',
            complete:  function(xhr){
            alert(+xhr.status);
                if(typeof cb === 'function')
                   cb.apply(this, [xhr.status]);

            }
        });
    }


    }// end of main

    </script>




    </head>
    <body>
    <button onclick="testFunction()">Click me</button>
    </html>

最佳答案

如果您使用外部 URL,此方法将不起作用,因为 CORS(跨源资源共享)会启动并阻止您,因为您不在同一域中。

工作版本:仅限本地文件

UrlExists('/path/file.php', function(status){
    if(status === 200){
       alert('file found');
    }
    else if(status === 404){
       alert('file not found');
    }
});

不幸的是,没有有效的方法通过 JavaScript 来做到这一点。但是,您可以通过后端功能来执行此操作,例如PHP

$file = 'http://www.othername.com/somefile.jpg';
$file_headers = @get_headers($file);

if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}

如果您将其构建到 PHP 函数中,然后使用 Ajax 功能将 URL 传递给 PHP 进行验证,然后返回响应 - 它应该可以工作。

编辑:Curl 示例

$mainUrl = curl_init($url);
curl_setopt($mainUrl,  CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($mainUrl);

$httpCode = curl_getinfo($mainUrl, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
    echo "404 Error";
}else if($httpCode == 200){
    echo "200 Error";
}else{
    echo "all good - sort off...";
}

curl_close($mainUrl);

这里是 Curl 选项 - 现在我要做的方式(而且我真的必须这样做......)是循环遍历页面上的每个 URL(在 js 中)并将其作为对象发送到PHP(通过 Ajax)。对于 PHP,我将使用上述 CURL 功能来确认哪些损坏(1 或 0),然后发回响应。

关于javascript - 使用 Javascript 检查 m3u8 URL 是否损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32607953/

相关文章:

.net - Ajax NumericUpDown 扩展器按钮布局

javascript - 如何在 API 调用中传递 key 作为授权 header

javascript - Casperjs 使用 casper.each 迭代链接列表

javascript - toDataURL 抛出未捕获的安全异常

javascript - 变量赋值会阻止删除项目吗?

javascript - Asp.Net MVC Ajax View 模型未更新

javascript - 使用 jquery 在加载页面上的 strust2 中自动完成

javascript - 当 'blur' 事件发生时,我如何找出哪个元素焦点*到*了?

javascript - 如何卡住CKEditor插件创建的html

Jquery Mobile 水平按钮