VUE+Element+vue-resource数据源请求

vue-resource的跨域请求

结果:前后端分离,前端用vue和vue-resource请求http接口地址获取数据

首先要安装vue-resource:

npm install vue-resource

然后在入口文件main.js中引入vue-source:

import VueResource from 'vue-resource'
Vue.use(VueResource)

准备工作到这里就做完了。

用来准备测试的接口:

http://ip.tianqiapi.com/?ip=

可以查询IP的一些简单信息,支持多个IP查询,IP之间用半角逗号间隔。该接口返回IP的JSON格式的信息。
vue文件中HTML部分的代码如下,使用了ElementUI的输入框和表格:

<template>
    <div>
        <el-input v-model="input1" id="inputText"></el-input>
        <el-button type="primary" @click="getData()">查询IP信息</el-button>
        <br>
            <el-table :data="data1" stripe>
                <el-table-column prop="ip" label="IP地址" width="140">
                </el-table-column>
                <el-table-column prop="country" label="国家" width="120">
                </el-table-column>
                <el-table-column prop="province" label="省份">
                </el-table-column>
                <el-table-column prop="city" label="城市">
                </el-table-column>
                <el-table-column prop="isp" label="服务供应商">
                </el-table-column>
            </el-table>
    </div>
</template>

js部分代码

<script>
    export default {
        name: "xuanxiang2",
        data(){
            return {
                data1 : [],
                input:''
            }
        },
        methods:{
            getData(){
                var text = document.getElementById("inputText").value;
                var api = "http://ip.tianqiapi.com/?ip="+text;//查询IP地址
                this.$http.get(api).then(function (response) {
                    console.log(typeof response.body);
                    if(typeof response.body == "object"){
                        var tmp=[];
                        for(var key in response.body){
                            //key是属性,object[key]是值
                            tmp.push(response.body[key]);//往数组中放值
                        }
                        this.data1 = tmp;
                    } else
                        this.data1 = response.body;
                },function (err) {
                    console.log(err);
                })
            }
        },
        mounted(){
            // this.getData();
        },
    }
</script>

this.$http.get即为发起get请求的语法,其他的还有:

get请求:

this.$http.get(URL,{
	    params:{
	    key:value,
    }
    }).then(function(success){
   		 //TODO: 成功后的操作
    },function(error){
   		 //TODO:失败后的操作
    })

post请求:

this.$http.post(URL,{
	params:{
	key:value,
}
}).then(function(success){
	//TODO: 成功后的操作
},function(error){
	//TODO:失败后的操作
})

jsonp请求:

this.$http.jsonp(URL,{
    params:{
    key:value,
}
}).then(function(success){
	//TODO: 成功后的操作
},function(error){
	//TODO:失败后的操作
})

js中data(){return{ }}中,是对变量的初始化

data(){
   return {
     data1 : [],  //定义一个叫data1的变量,[]表示其类型为Array
     input1:''  //定义一个叫input1的变量,''表示其类型为String
   }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeartBest丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值