05.vue-resource的基本使用

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue.js"></script>
    <!-- 注意:vue-resource依赖于vue,所以在引用时要注意顺序 -->
    <!-- this.$http -->
    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>

</head>

<body>
    <div id="app">
        <input type="button" value="get" @click="getInfo">
        <input type="button" value="post" @click="postInfo">
        <input type="button" value="jsonp" @click="jsonpInfo">
    </div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {

        },
        methods: {
            getInfo() {
                //通过.then来是指成功的回调函数
                this.$http.get('https://cn.vuejs.org').then(function(result) {
                    //通过result.body拿到服务器返回成功的数据
                    //console.log(result.body);
                })
            },
            postInfo() { //发起post请求
                //手动发起的post请求,默认没有表单格式,所以有的服务器处理不了
                //通过post方法的第三个参数,设置提交内容的类型为普通表单数据格式
                this.$http.post('http://vue-studyit.io/api/post', {}, {
                    emulateJSON: true
                }).then(function(result) {
                    //console.log(result.body)
                })
            },
            jsonpInfo() { //发起jsonp 请求
                this.$http.jsonp('http://vue-studyit.io/api/jsonp').then(result => {
                    console.log(result.body)
                })
            }
        }
    })
</script>

</html>

猜你喜欢

转载自www.cnblogs.com/LittleDuan/p/11295425.html