项目环境
代码部分
var HTTP = require('http')
var server = HTTP.createServer(function(req,res){
res.setHeader("Access-Control-Allow-Origin", "*");
var navData = [
{
title:'首页',
id:001
},
{
title:'历史',
id:002
},
{
title:'我的',
id:003
}
]
var data = JSON.stringify(navData)
if(req.url == '/api/navdata') {
res.end(data)
}
})
server.listen(8001,function(){
console.log('8001端口开启成功')
})
前端请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<p v-for="item in navList">{{item.title}}</p>
</div>
</body>
<script src="./vue.js"></script>
<script>
var vm= new Vue({
el: '#app',
data() {
return {
navList: [],
}
},
mounted() {
var xmlhttp = new XMLHttpRequest()
var that = this
xmlhttp.open("GET","http://127.0.0.1:8001/api/navdata", true);
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(JSON.parse(xmlhttp.responseText))
that.navList = JSON.parse(xmlhttp.responseText)
}
}
}
})
</script>
</html>
页面渲染呈现
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/69fdabdc88f6e7e30df65ac8247982e8.png)