Vue.js AJAX 跨域调用 JSONP | jQuery | vue-resource | axios

news/2024/7/10 2:56:32 标签: vue

Vue.js AJAX 跨域调用 JSONP

预览地址:http://djk8888.byethost32.com/VueAjaxJsonP/index.html

jQuery:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Vue + jQuery AJAX</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>

<body>
    <div id="app">
        <table border="1">
            <tr>
                <th>编号</th>
                <th>姓名</th>
            </tr>
            <tr v-for="people in peoples">
                <td>{{people.id}}</td>
                <td>{{people.name}}</td>
            </tr>
        </table>
    </div>

</body>

</html>
<script>
    var vv = new Vue({
        el: "#app",
        data: {
            peoples: [],
        },
        created: function () {
            this.bind();
        },
        methods: {
            bind: function () {
                $.ajax({
                    type: "get",
                    dataType: "jsonp",
                    jsonp: "callback",
                    jsonpCallback:"callback",
                    url: "http://djk8888.byethost32.com/VueAjax/jsonp.html",
                    success: function (data) {
                        $.each(data, function (index, item) {
                            var sth = { id: item.id, name: item.name };
                            vv.peoples.push(sth);
                        });
                    }
                });
            },
        }
    });
</script>

预览地址:http://djk8888.byethost32.com/VueAjaxJsonP/jQuery.html

vue-resource:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Vue.js Ajax(vue-resource)</title>
    <script src="https://cdn.staticfile.org/vue/2.4.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">
        <table border="1">
            <tr>
                <th>编号</th>
                <th>姓名</th>
            </tr>
            <tr v-for="people in peoples">
                <td>{{people.id}}</td>
                <td>{{people.name}}</td>
            </tr>
        </table>
    </div>
    <script>
        var vv = new Vue({
            el: "#app",
            data: {
                peoples: [],
            },
            created: function () {
                this.bind();
            },
            methods: {
                bind: function () {
                    this.$http.jsonp('http://djk8888.byethost32.com/VueAjax/jsonp.html', { jsonpCallback: "callback" }).then(function (res) {
                        for (var index in res.data) {
                            var sth = { id: res.data[index].id, name: res.data[index].name };
                            vv.peoples.push(sth);
                        }
                    }, function () {
                        console.log('请求失败处理');
                    });
                },
            }
        });
    </script>

</body>

</html>

预览地址:http://djk8888.byethost32.com/VueAjaxJsonP/resource.html

axios(axios本身不支持调用jsonp,通过原生javascript的ajax来调用jsonp,当然也可以用jQuery来调用)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Vue.js Ajax(axios)</title>
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>

<body>
    <div id="app">
        <table border="1">
            <tr>
                <th>编号</th>
                <th>姓名</th>
            </tr>
            <tr v-for="people in peoples">
                <td>{{people.id}}</td>
                <td>{{people.name}}</td>
            </tr>
        </table>
    </div>
    <script>
        var vv = new Vue({
            el: "#app",
            data: {
                peoples: [],
            },
            created: function () {
                this.bind();
            },
            methods: {
                bind: function () {
                    js_ajax({
                        url: "http://djk8888.byethost32.com/VueAjax/jsonp.html",  // 请求地址
                        jsonp: "callback", // 采用jsonp请求,且回调函数名为"jsonpCallbak",可以设置为合法的字符串    
                        data: {},  // 传输数据
                        success: function (data) {  // 请求成功的回调函数         
                            for (var index in data) {
                                var sth = { id: data[index].id, name: data[index].name };
                                vv.peoples.push(sth);
                            }
                        },
                        error: function (error) { }  // 请求失败的回调函数
                    });
                },
            },
        });
        //原生javascript的ajax调用jsonp
        function js_ajax(params) {
            params = params || {};
            params.data = params.data || {};
            var json = params.jsonp ? jsonp(params) : json(params);
            // jsonp请求  
            function jsonp(params) {
                //创建script标签并加入到页面中  
                var callbackName = params.jsonp || params.jsonpCallback;
                var head = document.getElementsByTagName('head')[0];
                // 设置传递给后台的回调参数名  
                params.data['callback'] = callbackName;
                var data = formatParams(params.data);
                var script = document.createElement('script');
                head.appendChild(script);
                //创建jsonp回调函数  
                window[callbackName] = function (json) {
                    head.removeChild(script);
                    clearTimeout(script.timer);
                    window[callbackName] = null;
                    params.success && params.success(json);
                };
                //发送请求  
                script.src = params.url + '?' + data;
                //为了得知此次请求是否成功,设置超时处理  
                if (params.time) {
                    script.timer = setTimeout(function () {
                        window[callbackName] = null;
                        head.removeChild(script);
                        params.error && params.error({
                            message: '超时'
                        });
                    }, time);
                }
            };
            //格式化参数  
            function formatParams(data) {
                var arr = [];
                for (var name in data) {
                    arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));
                };
                // 添加一个随机数,防止缓存  
                arr.push('v=' + random());
                return arr.join('&');
            }
            // 获取随机数  
            function random() {
                return Math.floor(Math.random() * 10000 + 500);
            }
        }
    </script>
</body>

</html>

预览地址:http://djk8888.byethost32.com/VueAjaxJsonP/axios.html

 

相关文章:

Vue.js Ajax(jQuery) | Vue.js Ajax(vue-resource) | Vue.js Ajax(axios) https://blog.csdn.net/djk8888/article/details/106012045 


http://www.niftyadmin.cn/n/836584.html

相关文章

linux 常用命令top、awk、sed等

1、watch命令 &#xff1a;周期性的执行下个程序&#xff0c;并全屏显示执行的结果 -n&#xff1a;指定周期秒数 -d&#xff1a;高亮显示变动过的地方 -t&#xff1a;关闭watch 命令在顶部的时间间隔命令显示 示例&#xff1a; watch -n 1 -d netstat -ant watch -n 1 -d pstr…

bcdedit添加linux引导,利用Bcdedit创建Linux系统引导

Bcdedit在Windows Vista中的一个命令行工具,用于建立和重新配置bootloader,无须再使用boot.ini文件.下面我们来谈谈如何用bcdedit引导Linux.1、概述之前xp时代可通过修改系统分区下(c:)的隐藏系统文件boot.ini来引导其他系统vista时代使用了全新的引导方式,因此配置不同&#x…

如何下载B站(bilibili)的视频

方法一&#xff0c;在bilibili后面添加jj&#xff1a; 如链接&#xff1a;https://www.bilibili.com/video/BV1kA411q7yB?spm_id_from333.851.b_7265706f7274466972737432.5 添加jj后&#xff1a;https://www.bilibilijj.com/video/BV1kA411q7yB?spm_id_from333.851.b_7265…

纯Vue.js 写的一个 自动轮播 Banner 图片广告 的小模块 练练手

自己写着玩 练手用的...(&#xff03;Д)... 在线预览地址&#xff1a;http://djk8888.byethost32.com/banner/banner_vue.html &#xff08;国外服务器 图片加载速度忒慢 ...("▔□▔)...等30秒...&#xff09; HTML页面&#xff1a; <!DOCTYPE html> <html…

linux服务器开机提示f1,linux服务器出现严重故障后的原因以及解决方法

2013-11-27 15:17 狂师阅读(303)评论(0) 编辑 收藏1、把系统安装光盘插入&#xff0c;重启机器&#xff0c;启动时迅速按下Del键&#xff0c;进入CMOS,把启动顺序改为光盘先启动&#xff0c;这样就启动了Linux安装程序&#xff0c;按F5,按提示打入Linux rescue回车&#xff0c;…

设计模式之禅——状态模式

我们每一个人都乘过电梯。电梯的动作&#xff1a;开门、关门、执行、停止。 如今我们用程序来实现一个电梯的动作&#xff0c;先看类图设计&#xff0c;如图所看到的 如今看一下代码 public interface ILift {//开启电梯public void open();//关闭电梯public void close();//能…

hive语句优化-通过groupby实现distinct(数据量特别大的时候,使用distinct去重容易导致数据倾斜)

hive语句优化-通过groupby实现distinct 同事写了个hive的sql语句&#xff0c;执行效率特别慢&#xff0c;跑了一个多小时程序只是map完了&#xff0c;reduce进行到20%。 该Hive语句如下&#xff1a; select count(distinct ip) from (select ip as ip from comprehensive.f_…

Redis 代理服务Twemproxy(redis分布式中间件)

文章来源&#xff1a;http://blog.csdn.net/hguisu/article/details/9174459/ 1、twemproxy explore 当我们有大量 Redis 或 Memcached 的时候&#xff0c;通常只能通过客户端的一些数据分配算法&#xff08;比如一致性哈希&#xff09;&#xff0c;来实现集群存储的特性。虽然…