<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
<script src="http://mockjs.com/dist/mock.js"></script>
</head>
<body>
<div id="box">
<input type="button" @click="get()" value="点我异步获取数据(Get)">
<input type="button" @click="get1()" value="点我异步获取数据(Get1)">
</div>
<script type = "text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
data:{
msg:'Hello World!',
},
methods:{
get:function(){
//发送get请求
this.$http.get('/try/ajax/ajax_info.txt').then(function(res){
document.write(res.body);
},function(){
console.log('请求失败处理');
});
},
get1:function(){
//测试一下mock
var data = Mock.mock({
name: {
first: '@FIRST',
middle: '@FIRST',
last: '@LAST',
full: '@first @middle @last'
}
});
JSON.stringify(data, null, 2);
// //发送get请求
//#拦截请求,返回模拟数据
Mock.mock('/try/ajax/ajax_info.txt', {
'list|3-8': [{'id|+3': 1}]
});
this.$http.get('/try/ajax/ajax_info.txt').then(function(res){
document.write(JSON.stringify(res.body));
},function(){
console.log('请求失败处理');
});
}
}
});
}
</script>
</body>
</html>
mock数据的使用
最新推荐文章于 2025-04-01 15:16:52 发布