ajax与nodejs实现小交互(html,txt)

来源:互联网 发布:百分百营销软件破解 编辑:程序博客网 时间:2024/04/30 08:00

ajax.js

//此组件仅实现了html文档的读取,并未实现xml的读取var btn = document.querySelector("#clickMe");var http_request = null;/**客户端的请求程序**/function makeRequest(url) {    //创建客户端xml对象    if (window.XMLHttpRequest) {        http_request = new XMLHttpRequest();        if (http_request.overrideMimeType) {            //解决火狐兼容性            http_request.overrideMimeType("text/xml");        }    } else if (window.ActiveXObject) { //ie不同版本的创建对象        try {            http_request = new ActiveXObject("Msxml2.XMLHTTP");        } catch(e) {            try {                http_request = new ActiveXObject("Microsoft.XMLHTTP");            } catch(e) {}        }    }    //若创建XML对象不成功,则提示。    if (!http_request) {        console.log("创建客户端xml对象不成功!终止。")        //终止函数        return false;    }     http_request.onreadystatechange = alertContent;    http_request.open('GET', url, true);    http_request.send(null);}/**处理服务器的响应**/function alertContent() {    if (http_request.readyState == 4) { //若收到响应        if (http_request.status == 200) {            // 弹出ajax获取到的数据            alert(http_request.responseText);        } else {            console.log("收到响应,但状态码不是200,而是" + http_request.status);        }    } else {        console.log("未收到响应,readyState不为4,为:" + http_request.readyState);    }   }/**按钮的监听事件**/btn.addEventListener("click",function(){    makeRequest("/html/content.txt");}, false);

server.js

var http = require("http");var fs = require("fs");var path = require("path");var mime = require("mime");var cache = {};// 请求不到资源,则发送404错误function send404(response) {    response.writeHead(404, {"Content-type": "text/plain"});    response.write("Error 404: Oh my god! The resource not found.");    response.end();}//若不是404,则发送文件function sendFile(response, filePath, fileContents) {    response.writeHead(        200,        {"Content-type": mime.lookup(path.basename(filePath))}    );    response.end(fileContents);}//静态文件服务,内部包含发送文件和404function serveStatic(response, cache, absPath) {    //若缓存中存在,则从缓存中读取    if (cache[absPath]) {        sendFile(response, absPath, cache[absPath]);    } else {        // 检查文件是否存在        fs.exists(absPath, function(exists){            if (exists) {   //若存在,则读取                console.log("成功找到的路径为" + absPath);                fs.readFile(absPath, function(err, data){                    if (err) {                        send404(response);                    } else {                        cache[absPath] = data;                        sendFile(response, absPath, data);                    }                });            } else {                console.log("断点,力求寻找的路径为" + absPath);                send404(response);            }        });    }}//创建服务器var server = http.createServer(function(request, response){    console.log("server start.")    var filePath;    //设定网站前台的根目录    if (request.url == "/") {        filePath = "/html/index.html";    } else {        filePath = request.url;  //js img html css    }    var absPath = "./" + filePath;    serveStatic(response, cache, absPath);}).listen(3000);

文件目录:

sqf

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 毕业了要搬宿舍怎么办 中专学历认证已停止怎么办 中专不做学历认证考试怎么办 大学生欠学费被扣毕业证怎么办 考警校体检没过怎么办 美国签证申请预约名字写错怎么办 当兵不从学校走怎么办 门牙崩了一小块怎么办 遇到很难过的事情怎么办 小孩子上课精力不集中怎么办 每天工作都很累压力大怎么办 重体力活搬不动怎么办 大学没参加体测怎么办 英文写的很丑怎么办 患有勃起障碍应该怎么办较好 运动过度小腿肌肉酸痛怎么办 高考有纹身是字怎么办 新生儿测听力没过关怎么办 色弱高考体检时没查出来怎么办 公司福利体检查二对半怎么办 高考体检表复印件丢了怎么办 高考体检表身高填错了怎么办 大学档案高考体检表丢了怎么办 工厂组织体检我有乙肝怎么办 我有乙肝单位组织体检怎么办? 矮腰袜子老掉怎么办 短腰袜子老下滑怎么办 中考体检结果丢了怎么办 咳嗽左胸围一处刺痛怎么办? 阴茎小父母催婚怎么办 头发扎进指甲缝怎么办 指甲缝扎流血了怎么办 中考考差了高中怎么办 骨折后我抽烟了怎么办 五年级科学考不好怎么办 考试连续考差了怎么办 客户说没考虑好怎么办 小孩生殖器痒经常用手抓怎么办 孩子在幼儿园生殖器官受伤怎么办? 被骗了3000块钱怎么办 小说 月经来了想体检怎么办