1. <script>
  2. var result = document.getElementById("result")
  3. result.addEventListener("mouseover",function(){
  4. const xhr = new XMLHttpRequest();
  5. xhr.open("POST","http://127.0.0.1:8000/server?a=100&b=200&c=300")
  6. xhr.send()
  7. xhr.onreadystatechange = function(){
  8. if(xhr.readyState === 4){
  9. if(xhr.status>=200&&xhr.status<300){
  10. result.innerHTML = xhr.response;
  11. }
  12. }
  13. }
  14. })
  15. </script>
  16. 对应js
  17. app.get('/server',(request,response)=>{
  18. //设置响应头 设置允许跨域
  19. response.setHeader('Access-Control-Allow-Origin','*')
  20. //设置响应体
  21. response.send('hello ajax')
  22. })

设置参数

  1. xhr.send('a=100&b=200&c=300')

image.png