Ajax--向服务器端发送请求、接收服务器响应数据
Ajax的实现步骤
1、创建Ajax对象
var xhr=new XMLHttpRequest();
2、告诉Ajax请求地址及请求方式
xhr.open('get','http://www.example.com');
3、发送请求
xhr.send();
4、获取服务器端给予客户端的响应数据
xhr.onload=function(){
console.log(xhr.responseText);
}
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <script type="text/javascript"> 9 var xhr=new XMLHttpRequest(); 10 xhr.open('get','http://localhost:3000/first'); 11 xhr.send(); 12 xhr.onload=function(){ 13 console.log(xhr.responseText) 14 } 15 </script> 16 </body> 17 </html>
app.js
1 //引入express框架 2 const express=require('express') 3 4 //引入路径处理模块 5 const path=require('path') 6 7 //创建web服务器 8 const app=express(); 9 10 //静态资源访问服务器功能 11 app.use(express.static(path.join(__dirname,'public'))) 12 13 app.get('/first',(req,res)=>{ 14 res.send('Hello Ajax'); 15 }) 16 //监听端口 17 app.listen(3000); 18 19 //控制台提示输出 20 console.log('服务器启动成功1')