本篇博客主要说明:
前后端框架(本例中是vue和koa)如何发送请求?获取响应?
以及跨域问题如何解决?
vue部分:
import App from './App.vue'
import Axios from 'axios'
new Vue({
el: '#app',
render: h => h(App),
mounted(){
Axios({
method: 'get',
url: 'http://localhost:3000',
}).then((response) => {
console.log(response);
})
}
})
koa部分:
const Koa = require('koa');
const cors = require('koa-cors');
const app = new Koa();
const main = ctx => {
ctx.response.body = 'Hello World';
};
app.use(cors());
app.use(main);
app.listen(3000);
console.l