Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取文件';不带JQuery的Javascript中的内容_Javascript_Xmlhttprequest - Fatal编程技术网

如何获取文件';不带JQuery的Javascript中的内容

如何获取文件';不带JQuery的Javascript中的内容,javascript,xmlhttprequest,Javascript,Xmlhttprequest,通常,为了在网页内完成某些任务,需要将存储在服务器中的文件中的内容加载到Javascript变量中,或将其显示在HTML元素中 如果不依赖JQuery,我怎么能做到这一点呢?我找到的最简单的方法之一是创建一个函数,获取一个文件,并在下载完成后调用另一个函数。 因此,在下面的示例中,当加载“test.txt”文件内容时,它将显示在pre元素中 <html> <body> <pre id="output"></pre> </body> &l

通常,为了在网页内完成某些任务,需要将存储在服务器中的文件中的内容加载到Javascript变量中,或将其显示在HTML元素中


如果不依赖JQuery,我怎么能做到这一点呢?

我找到的最简单的方法之一是创建一个函数,获取一个文件,并在下载完成后调用另一个函数。 因此,在下面的示例中,当加载“test.txt”文件内容时,它将显示在pre元素中

<html>
<body>
<pre id="output"></pre>
</body>
<script type="text/javascript">

function get_file(url, callback)
{
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.send();
}

get_file("test.txt", function(response)
{
    document.getElementById("output").innerHTML=response;
});

</script>
</html>


但这将以在加载数据之前挂起网页为代价。

我找到的最简单的方法之一是创建一个函数来获取文件,并在下载完成后调用另一个函数。 因此,在下面的示例中,当加载“test.txt”文件内容时,它将显示在pre元素中

<html>
<body>
<pre id="output"></pre>
</body>
<script type="text/javascript">

function get_file(url, callback)
{
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.send();
}

get_file("test.txt", function(response)
{
    document.getElementById("output").innerHTML=response;
});

</script>
</html>

但这将以在加载数据之前挂起网页为代价。

尝试使用fetch:

它比XMLHttpRequest更易于使用

然后,获取资源后,使用insertAdjacentHtml将其添加到文档正文中:

i、 e

fetch(“test.json”)
.然后(功能(响应){
返回response.json();
})
.then(函数(json){
document.body.insertAdjacentHtml(“afterbegin”,“p>”+json+”

); }); .catch(函数(错误){ console.log('提取操作出现问题:'+error.message); });
尝试使用fetch:

它比XMLHttpRequest更易于使用

然后,获取资源后,使用insertAdjacentHtml将其添加到文档正文中:

i、 e

fetch(“test.json”)
.然后(功能(响应){
返回response.json();
})
.then(函数(json){
document.body.insertAdjacentHtml(“afterbegin”,“p>”+json+”

); }); .catch(函数(错误){ console.log('提取操作出现问题:'+error.message); });
xmlhttp.open("GET", url, false);
fetch("test.json")
  .then(function(response) {
           return response.json();
        })
  .then(function(json) {
           document.body.insertAdjacentHtml("afterbegin", "<p>" + json + "</p>);
        });
  .catch(function(error) {
             console.log('There has been a problem with your fetch operation: ' + error.message);
          });