需要获取静态文件的状态

XueQing

我需要在页面上获取静态文件的状态(http代码)。

例如html代码包含

<link rel="stylesheet" type="text/css" href="theme.css">
<link rel="stylesheet" type="text/css" href="https://example.com/theme.css">
<script src="script.js"></script>
<script src="https://example.com/script.js"></script>
<img src="image.png">
<img src="https://example.com/image.png">

在输出上需要获取这样的json仅需要404。

{
  "css": [
    {
      "https://example.com/theme.css": "404",
      "https://example2.com/theme.css": "404"
    }
  ],
    "js": [
    {
      "https://example1.com/script.js": "404",
      "https://example.com/script.js": "404"
    }
  ],
      "images": [
    {
      "https://example.com/image.png": "404",
      "https://example.com/image.png": "404"
    }
  ]
}

必须仅使用Javascript完成。

我要用这个

var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
        callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);

有必要在收到状态后,不要通过对文件执行单独的GET请求来完成此操作。

但是我找不到执行单独的GET请求的方法。

谁可以找到解决方案?

里特什·坎德卡(Ritesh Khandekar)

如果您使用的是PHP等服务器端语言,那么我有一些想法。例如。

<?php
$url= $_POST["url"];
$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_NOBODY => true)); $header = explode("\n", curl_exec($curl)); curl_close($curl); $string = $header[0];
if(strpos($string, "200")){
    echo "200";
}else{
    echo "404";
}
?>

使用url参数在AJAX中调用上述文件

在服务器端类似的行。像(python):

s=socket.socket()
s.connect('url',80)
s.send('GET /path HTTP/1.0\r\nHost:hostname\r\n\r\n')
print s.recv(1024) #some lines of headers

简而言之,根据以上内容,status code如果不发送,就不可能获取任何主机中存在的文件request

因此,不发送多个AJAX请求(单独的GET请求)是不可能的

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章