APICloud AJAX请求数据小案例

2019-04-30  本文已影响0人  挤时间学习的阿龙

api对象调用-方法-参数-传参-回调

APICloud api组件文档 https://docs.apicloud.com/Client-API/api#3
1、示例代码

// 表单方式提交数据或文件
api.ajax({
    url: 'http://192.168.1.101:3101/upLoad',
    method: 'post',
    data: {
        values: {
            name: 'haha'
        },
        files: {
            file: 'fs://a.gif'
        }
    }
}, function(ret, err) {
    if (ret) {
        api.alert({ msg: JSON.stringify(ret) });
    } else {
        api.alert({ msg: JSON.stringify(err) });
    }
});

// 提交JSON数据
api.ajax({
    url: 'http://192.168.1.101:3101/upLoad',
    method: 'post',
    headers: {
        'Content-Type': 'application/json;charset=utf-8'
    },
    data: {
        body: {
            name: 'haha'
        }
    }
}, function(ret, err) {
    if (ret) {
        api.alert({ msg: JSON.stringify(ret) });
    } else {
        api.alert({ msg: JSON.stringify(err) });
    }
});

2、APICloud ajax请求数据

        methods: {
            init: function(){
                $api.css($api.byId("app"));
                api.ajax({
                    url: index_url,
                    method: 'post',
                    timeout:30,
                    dataType:'json',
                    returnAll:false,
                    data: {

                    }
                }, function(ret, err) {
                    if (ret) {
                        console.log(JSON.stringify(ret));
                        vm.ads1_list = ret.data.ads1_list;
                        vm.ads2_list = ret.data.ads2_list;
                        vm.goods_list = ret.data.goods_list;
                    } else {
                        console.log(JSON.stringify(err));
                    }
                });
            }
        }

    });
    setTimeout(function () {
        vm.init();
    },500)

3、效果展示

image.png

总结:
要同步到手机或者模拟器测试,因为api必须在它的壳里运营。
电脑运行报 api is not defined


image.png
上一篇下一篇

猜你喜欢

热点阅读