引入vue-resource
<script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script>
post请求
postmethod(){
//发起post请求, application/x-www-form-urlencoded
//手动发起 post 请求,默认没有表单格式,所以,有的 服务器处理不了
//通过post 方法的三个参数,设置提交内容类型为普通表单数据提交格式
this.$http.post('http://localhost:9091/getList',{},{emulateJSON:true}).then(result=>{
console.log(result)
})
},
get请求
getMethod(){//发起get请求
this.$http.get('http://localhost:9091/getList').then( function (result) {
console.log(result)
})
}
jsonp请求
jsonpMethod(){
this.$http.jsonp('http://vue.studyit.io/api/jsonp').then(result => {
console.log(result.body)
})
案例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--//导入js包-->
<script src="js/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script>
</head>
<body>
<div id="app">
<input type="button" value="post请求" @click="postmethod">
<input type="button" value="get请求" @click="getMethod">
<input type="button" value="jsonp请求" @click="jsonpMethod">
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
msg: 'hellfo vue.js.'
},
methods:{
postmethod(){
//发起post请求, application/x-www-form-urlencoded
//手动发起 post 请求,默认没有表单格式,所以,有的 服务器处理不了
//通过post 方法的三个参数,设置提交内容类型为普通表单数据提交格式
this.$http.post('http://localhost:9091/getList',{},{emulateJSON:true}).then(result=>{
console.log(result)
})
},
getMethod(){//发起get请求
this.$http.get('http://localhost:9091/getList').then( function (result) {
console.log(result)
})
},
jsonpMethod(){
this.$http.jsonp('http://vue.studyit.io/api/jsonp').then(result => {
console.log(result.body)
})
}
}
});
</script>
</body>
</html>