马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wp231957 于 2020-5-7 15:48 编辑
前端:H5+VUE+AXIOS(非项目工程,就是一个单独的HTML文档)
网上很多资料都是修改配置文件,使用代理,可是我不是web工程,该咋修改代理?<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title>vue+axios测试</title>
</head>
<body>
<div id="w_1">
<ul>
<li v-for="item in jokes">{{item}}</li>
</ul>
<br><input type="button" value="get" class="get" v-on:click="getjoke">
</div>
<script>
var url = "http://api.zhuishushenqi.com/ranking/gender";
var app = new Vue({
el: "#w_1",
data: {
jokes: ["狠好看的笑话"]
},
methods: {
getjoke: function(event) {
let that = this;
axios.get(url).then((response) => {
console.log(response);
}, (err) => {
console.log(err);
});
//event.target.value="再来一批"
},
}
})
</script>
</body>
</html>
后台:NODE +EXPRESSvar express = require('express');
var http = require('http');
var fs = require('fs');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-type");
res.header("Access-Control-Allow-Methods", "GET");
next();
});
app.get('/', function(req, res, next) {
res.writeHead(200, { 'Content-Type': 'text/html' })
fs.readFile('./xiaoshuo.html', 'utf-8', function(err, data) {
if (err) {
throw err;
}
res.end(data);
});
});
app.listen(3000);
console.log("监听端口 3000");
//console.log(app);
测试环境:chrome
几张相关图片:
补充, var url = "http://api.zhuishushenqi.com/ranking/gender"; 这个我单独在浏览器(CHROME)运行是能拿到数据的
|