Ajax即Asynchronous JavaScript and XML(异步JavaScript和XML)的缩写 Vue要实现异步加载需要使用vue-resource库 引入vue-resource: 需要注意的是 vue-resource 依赖于Vue 因此引入的顺序要放在Vue的后面 vue-resource往Vue上面挂载了一个 全部请求: 请求返回的数据: 语法: 请求返回的数据: 语法: 请求返回的数据: 请求返回的数据: 在Vue中 除了使用vue-resource之外 还可使用axios实现实现数据的请求 Axios是一个基于Promise的HTTP库 可用于浏览器和node.js中 引入Axios: 可直接使用别名来发起请求: 请求返回的数据: axios的请求语法其实和vue-resource很相似 请求返回的数据: 语法: 请求返回的数据: 注: 请求返回的数据: 另: 笔者的测试接口用的是Java 解决方法:在绑定入参的时候加上 例: 特此记录一下
什么是Ajax
Ajax能够在不重新加载整个页面的情况下与服务器交换数据并更新部分网页一、使用vue-resource发起ajax请求
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
$http
因此 可通过this.$http
来发起请求 例如this.$http.get()
或this.$http.post()
等
通过.then()
获取返回的数据this.$http.get()/post().then(成功回调函数[必填],失败回调函数[可填])
使用例子:
<div id="app"> <input type="button" value="get" @click="getInfo"> </div>
<script> var vm=new Vue({ el:'#app', data:{}, methods:{ getInfo() { // 发起get请求 // 通过.then()设置请求成功后的回调函数 this.$http.get("http://localhost/testGet").then(function(result) { console.log(result); }) } } }); </script>
get
请求get(url, [config])
getInfo() { // 发起get请求 // 通过.then()设置请求成功后的回调函数 this.$http.get("http://localhost/testGet").then(function(result) { // 通过result.body拿到服务器返回的数据 console.log(result.body); }) }
post
请求post(url, [body], [config])
手动发起的post请求的请求头中默认是没有表单格式的 从而导致无法处理编码为application/json的请求
此时 可通过post方法的第三个参数(config) 设置emulateJSON为true 设置提交的内容类型为普通表单的数据格式postInfo() { // 携带参数发起post请求 this.$http.post("http://localhost/testPost",{},{emulateJSON:true}).then(result=>{ console.log(result.body); }) }
携带参数:
postArgsInfo() { // 发起post请求 this.$http.post("http://localhost/testPostArgs",{ page:1, keyword:"学习" },{ // 设置请求头 // emulateJSON:true }).then(result=>{ console.log(result.body); }) }
二、使用Axios发起ajax请求
Vue.js2.0版本推荐使用axios来完成ajax请求<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
使用例子:
<div id="app"> <input type="button" value="get" @click="getInfo"> </div>
<script> var vm=new Vue({ el:'#app', data:{}, methods:{ getInfo() { // 发起get请求 // 通过.then()设置请求成功后的回调函数 axios.get("http://localhost/testGet") .then(function(result){ // 通过result.data拿到服务器返回的数据 console.log(result); }) .catch(function(err) { console.log(err); // 请求失败处理 }) } } }); </script>
get
请求
语法:axios.get(url[, config])
getInfo() { // 发起get请求 // 通过.then()设置请求成功后的回调函数 axios.get("http://localhost/testGet") .then(function(result){ // 通过result.data拿到服务器返回的数据 console.log(result.data); }) .catch(function(err) { console.log(err); // 请求失败处理 }) }
post
请求axios.post(url[, data[, config]])
postInfo() { // 发起post请求 axios.post("http://localhost/testPost") .then(result=>{ // 通过result.data拿到服务器返回的数据 console.log(result.data); }) .catch(function(err) { console.log(err); // 请求失败处理 }) }
携带参数:
headers: { "Content-Type": "application/x-www-form-urlencoded" }
可设置请求头
请求数据若为JSON数据 则需在post()的第三个参数中设置该属性 否则会报错postArgsInfo() { // 携带参数发起post请求 axios.post("http://localhost/testPostArgs",{ page:1, keyword:"学习" },{ // 设置请求头 // headers: { "Content-Type": "application/x-www-form-urlencoded" } }).then(result=>{ // 通过result.data拿到服务器返回的数据 console.log(result.data); }).catch(function(err) { console.log(err); // 请求失败处理 }) }
在获取post传的参数的时候有个坑 在后台取不到传来的数据@RequestBody
注解即可public Map<String,Object> postAPI(@RequestBody Map data) { ... }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算