ajax一共就那么几步:
1.创建ajax
2.给ajax加事件
3.打开ajax
4.发送ajax
5.事件函数
没了!!
<script>
var ajax=new XMLHttpRequest();
ajax.addEventListener("load",loadHandler);
ajax.open("GET","http://localhost:4001?user=张三");
ajax.send();
function loadHandler(e){
console.log(ajax.response);
}
</script>
简单的nodejs创建的服务器
var http=require("http");
var querystring=require("querystring");
// 创建服务,参数是一个函数,函数中有两个参数
// 1\req叫做请求,2\res就是响应
var server=http.createServer(function(req,res){
// 请求消息接收数据的过程
req.on("data",function(){
});
// 请求消息接收数据完毕
req.on("end",function(){
if(req.url.indexOf("favicon.ico")>-1) return;//这个去chrome图标错误的不用管他
var user=querystring.parse(req.url.split("?")[1]).user;
// 并且给返回消息写入1
res.writeHead(200,{
"content-type":"text/html;charset=utf-8",
// 允许所有跨域请求,*是通配符
"Access-Control-Allow-Origin":"*"
})
res.write(user+"今年18岁");
// 结束发送
res.end();
})
});
// 创建好的服务开启侦听,第一个参数是网络的端口号,第二个参数是网络的ip,第三个参数是开启后的回调函数
server.listen(4001,"localhost",function(){
console.log("服务已开启");
})