const express =require('express');
const app=express();
app.get('/server',(request,response)=>{
response.setHeader("Access-Control-Allow-Origin","*");
response.send('Hello ajax1');
});
app.listen(8000,()=>{
console.log("服务8000");
});
测试:
// 获取对象
const result=document.getElementById('result');
// 绑定事件
result.addEventListener('mouseover',function () {
// 1.创建新对象
const xhr=new XMLHttpRequest();
xhr.open('POST','http://127.0.0.1:8000/server');
xhr.send();
//2.事件绑定
xhr.onreadystatechange=function(){
if (xhr.readyState===4) {
if (xhr.staus>=200 && xhr.staus<300) {
result.innerHTML=xhr.response;
}
}
}
});