vue笔记-08(vue-resource发起get,post,

2020-05-09  本文已影响0人  7ColorLotus
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue-resource演示</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
</head>
<body>
    <div id="app">
        <div>
            <input type="button" value="get" @click="get()">
            <input type="button" value="post" @click="post()">
            <input type="button" value="jsonp" @click="jsonp()">
        </div>
        <div>
            {{ content }}
        </div>
    </div>
    <script>
        //用于jsonp请求回调的函数
        function jsonpCall(){
            console.log("jsonp Call success")
        }


        var vm = new Vue({
            el: "#app",
            data: {
                content:''
            },
            methods:{
                get(){ //如果是静态文件直接运行,需要处理跨域问题
                    var url = "http://localhost:10439/say_hi"
                    this.$http.get(url, function(result){
                        this.content = result.body
                    },function(res){
                        console.log("请求失败")
                    })
                },

                post(){ //如果是静态文件直接运行,需要处理跨域问题
                    var url = "http://localhost:10439/say_hi_post"
                    var param = {data : 'test'}
                    this.$http.post(url, param , function(result){
                        this.content = result.body
                    },function(res){
                        console.log("请求失败")
                    })
                },

                jsonp(){ //前端处理跨域请求的一种方式,只有get请求
                    var url = "http://localhost:10439/jsonp_call"
                        this.$http.jsonp(url, function(result){
                        this.content = result.body
                    })
                }
            }
        })
    </script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读