Vue发送ajax请求 get/post/jsonp请求

2018-11-08  本文已影响117人  ___大鱼___

导包 下载地址 https://www.npmjs.com/package/vue-resource

<script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/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="get请求" @click="getInfo">
        <input type="button" value="post请求" @click="postInfo">
        <input type="button" value="jsonp请求" @click="jsonPInfo">


    </div>


    <script>

        var app = new Vue({
            el: '#app',
            data:{


            },
            methods: {
                getInfo(){
                    // get请求
                    this.$http.get('/dayu').then(response =>{
                        console.log('请求成功')
                    }, response=>{
                        console.log('请求失败')
                    })
                },
                postInfo(){ //post请求   application/x-www-form-urlencoded
                    // 手动发起的post请求. 默认没有表单格式, 所以, 有的服务器处理不了
                    // 设置参数 {'emulateJSON': true}  以防止请求失败

                    this.$http.post('', {}, {'emulateJSON': true}).then(response=>{

                    }, response=>{

                    })
                },
                jsonPInfo(){  // 发起jsonp请求
                  this.$http.jsonp('').then(result=>{
                      
                  }, result=>{
                      
                  })
                }

            },
            created: function () {

            },
            mounted: function () {

            }
        })
    </script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读