切换导航
简历
工具
演示
查询
导航菜单
服务端
30
前端
114
数据库
82
进阶开发
8
桌面程序
2
QA
15
软件使用
36
规范协议
5
操作系统
11
AI
0
复用资源
6
硬件
0
标签
HTML
正则表达式
Json
包管理工具
vite
CSS
XPath
JavaScript
Jquery
less
ES6
Vue
React
vue 3
Layui
editor.md
sea.js
Zepto.js
mysql.js
mssql.js
NodeJs
Express
前端template
YAML
杂项
外部链接
.NET Core SDK和运行时安装包
淘宝 NPM 镜像
查询网
Tables Generator线上表格转换工具
引用cdn在线脚本和样式
首页
前端
Vue请求resource
[TOC] ## Vue请求resource 介绍:vue-resource是vue请求服务器的一款插件,vue-resource依赖以vue.js,引用vue-resource.js要在vue.js之后,引用完之后vue-resource.js会往Vue实例中挂载一个$http的属性,点击[vue-resource.js](https://github.com/pagekit/vue-resource)查看说明 + 配置:[vue-resource配置说明](https://github.com/pagekit/vue-resource/blob/develop/docs/config.md) + API:[API使用说明](https://github.com/pagekit/vue-resource/blob/develop/docs/http.md) ### vue-resouce常用请求方式 + 在vue-resouce中发起get请求后,通过.then设置成功的回调函数,然后通过返回结果中的.body拿到服务器返回的数据 + 在vue-resource中发起post请求默认是没有表单格式(application/x-www-form-urlencoded),一定要设置emulateJSON:true,通过这个设置将提交的内容设置为普通的表单数据格式,同get一样使用.body拿到服务器返回的数据 + vue-resource发起请求最早也只能生命周期函数created()中,这个生命周期函数执行前vue刚好初始化完data和methods ### vue-resource全局设置 + 服务器请求的全局地址设置,Vue.http.options.root = "http://localhost:58347/",设置了全局根域名地址后,之后发起的每个请求都是一个相对路径,url路径前面不能带'/' + 通过设置Vue.http.options.emulateJSON = true 将请求服务器时传递的内容都设置为普通表单格式,全局都设置了emulateJSON:true ```HTML <html> <hred> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0, maximun-scale=1.0, minimun-scale=1.0"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.5.3/css/bootstrap.min.css"> <script src="https://cdn.bootcss.com/vue/2.5.2/vue.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.5.3/vue-resource.js"></script> </hred> <div id="app"> <div class="alert alert-primary"> <div> <h3>三国群英会</h3> </div> <div class="form-inline"> <label>名字:<input class="form-control" type="text" v-model="name" /></label> <label>性别:<input type="radio" id="man" value="0" v-model="sex"><label for="man">男</label> <input type="radio" id="woman" value="1" v-model="sex"><label for="woman">女</label> </label> <input type="button" class="btn btn-primary" value="添加" @click="add" /> <label>搜索名字:<input class="form-control" type="text" v-model="keyword" @keyup.enter="search" /></label> </div> </div> <table class="table table-bordered table-hover table-striped"> <thead> <tr> <th>ID</th> <th>名字</th> <th>性别</th> <th>时间</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="item in persons" :key="item.Id"> <td>{{item.Id}}</td> <td>{{item.Name}}</td> <td>{{item.Sex | sexchange}}</td> <td>{{item.AddDate | datetimeFormat}}</td> <td><a href="#" @click.prevent="del(item.Id)">删除</a></td> </tr> </tbody> </table> </div> <script type="text/javascript"> Vue.http.options.root = "http://localhost:58347/"; Vue.http.options.emulateJSON = true; var vm = new Vue({ el: "#app", data: { sex: "0", name: "", keyword: "", persons: [] }, methods: { add() { this.$http.post("api/VueStudy/AddPerson", { Name: this.name, Sex: this.sex }).then(item => { alert(item.body.msg) if (item.body.code == 0) { this.search("") this.sex = 0 this.name = ""; } }) }, del(id) { this.$http.post("api/VueStudy/DelPerson?Id=" + id).then(item => { alert(item.body.msg) if (item.body.code == 0) { this.search() } }) }, search() { this.$http.get("api/VueStudy/GetPersonInfos?keyword=" + this.keyword).then(item => { if (item.body.code == 0) { this.persons = item.body.data } else { alert(item.body.msg) } }) } }, created() { this.search() }, filters: { sexchange: function (str) { return str == 0 ? "男" : "女" }, datetimeFormat: function (str) { var date = new Date(str); var y = date.getFullYear().toString().padStart(2, "0"); var m = date.getMonth().toString().padStart(2, "0"); var d = date.getDay().toString().padStart(2, "0"); var hh = date.getHours().toString().padStart(2, "0"); var mm = date.getMinutes().toString().padStart(2, "0"); var ss = date.getSeconds().toString().padStart(2, "0"); return `${y}-${m}-${d} ${hh}:${mm}:${ss}`; } } }) </script> </html> ```
扫一扫手机看
点击排行
windows 文件资源管理器使用
Layui 弹窗open使用说明
SQL Server 中字符串函数使用
C# 使用QQ邮箱代理做邮件接收功能
SQL Server关于apply的两种形式cross apply 和 outer apply
ASP.NET MVC 模型的自定义验证
nvm安装及常用命令
SQL Server 中数据类型转换
SQL Server 中WITH AS短语的使用
IE浏览器下AJAX缓存问题导致数据不更新的解决办法
订阅号