【JS】Ajax请求本地文件

news/2024/7/19 15:58:14 标签: JS, Ajax, 请求, 本地文件, ajax请求本地文件

人懒话不多,上代码。

var btn = document.getElementById("btn");
btn.addEventListener("click",
  function() {
    var xhr =new XMLHttpRequest()
    xhr.open("GET", "data.txt", true);//注意路径问题读取data.txt文件的内容。
    xhr.send(null);
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
        if (xhr.status == 200||xhr.status==0) {//请求本地txt文件时状态码是0。
          alert(xhr.responseText);
        } else {
          alert(xhr.status);
        }
      }
    }
  }, false)

 


http://www.niftyadmin.cn/n/1499057.html

相关文章

RabbitMQ安装详解

安装(centos系统) 第一步: 下载rabbitmq安装包 第二步: 安装erlang 1) 安装Erlang Solutions仓库到你的系统(目的在于让你可以使用yum安装到最新版本的erlang, 如果不设置, yum安装的…

【VUE】在vue项目实践当中使用swiper轮播图教程

步骤: 1. 安装vue-awesome-swiper npm install vue-awesome-swiper -S 2.在vue项目中引用vue-awesome-swiper //main.jsimport VueAwesomeSwiper from vue-awesome-swiper // require styles import swiper/dist/css/swiper.css Vue.use(VueAwesomeSwiper, /* {…

C++基础题

刚在网上转看到几道对于巩固基础很有帮助的C基础题,反正闲着也是闲着,就做了下,具体题型如下:答案是我自己写,不一定对,如果有朋友看到不对的,欢迎指正,万分感谢!1. 一个…

sync不能成功同步

[rootlocalhost bin]# ./sync_ctrl start start the sync_d .... start the sync_d 0.... Target:sync_d 0 is running with pid 143899 ... start ok [rootlocalhost bin]# ./sync_ctrl status sync_d 0 is down命令启动后,没有真的启动,db_sync_short_…

KVM分析报告

转载KVM分析报告虚拟化技术工作组2008-12-311. 概述1.1. KVM简介KVM是以色列开源组织Qumranet开发的一个开源虚拟机监控器,从Linux-2.6.20开始被包含在Linux内核中。KVM基于x86硬件虚拟化技术,它的运行要求Intel VT-x或AMD SVM的支持。一般认为&am…

【CSS】css样式控制div水平垂直居中的六种方法

1. 绝对定位方法:不确定当前div的宽度和高度,采用 transform: translate(-50%,-50%); 当前div的父级添加相对定位(position: relative;) 如图所示: 代码如下: div{background:red;position: absolute;le…

[mark]C# 异常处理

https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/programming-guide/exceptions/index转载于:https://www.cnblogs.com/yuelien/p/6879619.html

【微信小程序】小程序检索词显示高亮

话不多少,上代码 html <view class="secondtypelistboxs"><block wx:for={{informationList.wiki.data}} wx:key=*this><navigator class="secondtypeitemboxs" url="../wiki/catalog_nextPage?id={{item.id}}" hover-class=&…