温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在vue中使用ajax获取后台数据并显示

发布时间:2021-04-12 18:04:05 来源:亿速云 阅读:364 作者:Leah 栏目:web开发

怎么在vue中使用ajax获取后台数据并显示?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

实例如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <script src="/vue.min.js"></script>
 <script src="/vue-resource.min.js"></script>
 <style>
  #th th{
   background-color: #50a9fa;
   color: aliceblue;
   font-size: large;
  }
 </style>
</head>
<body >
<div id="app" align="center">
 <input type="text" v-model="id">
 <input type="text" v-model="pname">
 <button @click="addData">添加数据</button>
 <table id="th" border="1"solid width="400px">
  <tr>
   <th>编号</th>
   <th>名称</th>
   <th>时间</th>
   <th>操作</th>
  </tr>
  <tr v-for="item in list ">
   <td>{{item.id}}</td>
   <td>{{item.name}}</td>
   <td>{{item.ctime}}</td>
   <td>
    <a href="javascript:void(0)" rel="external nofollow" >删除</a>
   </td>
  </tr>
 </table>
</div>
<script>
 //vue的生命周期
 
 new Vue({
  el:'#app',
  data:{
   list:[]
  },
  //vue对象实例创建成功之后就会自动调用这个方法
  //如果你想写的方法在舒适化的时候就被调用的话就要要用到created这个
  created:function () {
   this.getlist();//这里定义这个方法,vue实例之后运行到这里就调用这个函数
  },
  methods:{
   getlist:function () {
    //请求服务器的api获取到品牌的数据列表
    this.$http.get('http://vueapi.ittun.com/api/getprodlist').then(function (response) {
     //处理服务器异常信息提示
     if(response.body.status!=0)
     {
      alert(response.body.message);
      return ;
     }
     //处理正常的逻辑数据处理
     this.list=response.body.message;
     
    });
    

   }
   
  }
 });
</script>
</body>
</html>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI