客户端代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> //创建ajax对象 var xhr = new XMLHttpRequest(); //告诉ajax对象以什么方式想哪个地址发送请求 xhr.open('get','http://localhost:80/first'); //发送请求 xhr.send(); //当页面加载完之后拿到数据 xhr.onload = function(){ console.log(xhr.responseText); } </script> </body> </html>node代码
const express = require('express'); //引入系统模块path const path = require('path'); //创建服务器 const app = express(); app.use(express.static(path.join(__dirname,'public'))) //设置静态资源 app.get('/first',(req,res)=>{ res.send({name:'zs'}); }) app.listen(80); console.log('服务器创建成功');客户端拿到的后端数据默认是字符串 一般通过JSON.parse转成对象格式