赞
踩
后端代码 node.js:
//1. 引入express const { request, response } = require('express'); const express = require('express'); //2. 创建应用对象 const app = express(); //3. 创建路由规则 // request 是对请求报文的封装 // response 是对响应报文的封装 app.get('/server', (request, response)=>{ //设置响应 response.setHeader('Access-Control-Allow-Origin', '*'); response.send('HELLO AJAX'); }); app.post('/server', (request, response)=>{ //设置响应 response.setHeader('Access-Control-Allow-Origin', '*'); //响应头 response.setHeader('Access-Control-Allow-Headers', '*'); response.send('HELLO AJAX POST'); }); app.all('/server', (request, response)=>{ //设置响应 response.setHeader('Access-Control-Allow-Origin', '*'); //响应头 response.setHeader('Access-Control-Allow-Headers', '*'); response.send('HELLO AJAX POST'); }); app.all('/json-server', (request, response)=>{ //设置响应 response.setHeader('Access-Control-Allow-Origin', '*'); //响应头 response.setHeader('Access-Control-Allow-Headers', '*'); const data={ name:'kd', } let str=JSON.stringify(data) response.send(str); }); //延时响应 app.all('/delay',(request, response) => { //设置响应头 设置允许跨域 response.setHeader('Access-Control-Allow-Origin', '*'); response.setHeader('Access-Control-Allow-Headers', '*'); setTimeout(()=>{ response.send('延时响应') },1000) }) //axios服务 app.all('/axios-server', (request, response)=>{ //设置响应 response.setHeader('Access-Control-Allow-Origin', '*'); //响应头 response.setHeader('Access-Control-Allow-Headers', '*'); const data={ name:'kd', } let str=JSON.stringify(data) response.send(str); }); app.all('/fetch-server', (request, response)=>{ //设置响应 response.setHeader('Access-Control-Allow-Origin', '*'); //响应头 response.setHeader('Access-Control-Allow-Headers', '*'); const data={ name:'kd', } let str=JSON.stringify(data) response.send(str); }); //4. 监听端口启动服务 app.listen(8000, ()=>{ console.log("服务已经启动, 8000 端口监听中...."); });
前端代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>axios</title> <script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script> </head> <body> <button>GET</button> <button>POST</button> <button>AJAX</button> <script> const btns = document.querySelectorAll('button'); //配置 baseURL axios.defaults.baseURL = 'http://127.0.0.1:8000'; btns[0].onclick=function(){ axios.get('/axios-server',{ //url参数 params:{ id:100, vip:7 }, //请求头信息 headers:{ name:'kaiwen', age:35 } }).then(value=>{ console.log(value); }) } btns[2].onclick=function(){ axios({ method : 'POST', //url url: '/axios-server', //url参数 params: { vip:10, level:30 }, //头信息 headers: { a:100, b:200 }, //请求体参数 data: { username: 'admin', password: 'admin' } }).then(response=>{ console.log(response.data); console.log(response.status); }) } </script> </body> </html>
点击GET按钮后,
点击AJAX按钮后:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>fetch 发送ajax请求</title> </head> <body> <button>AJAX</button> <script> const btn=document.querySelector('button') btn.onclick=function(){ fetch('http://127.0.0.1:8000/fetch-server',{ //请求方法 method:'POST', //请求头 headers:{ name:'kd', }, //请求体 body:'username=admin$password=admin' }).then(response=>{ return response.json() }).then(response=>{ console.log(response); }) } </script> </body> </html>
点击按钮后
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。