json请求 post vue_vue基础之使用get、post、jsonp实现交互功能示例

本文实例讲述了vue基础之使用get、post、jsonp实现交互功能。分享给大家供大家参考,具体如下:

一、如果vue想做交互,引入: vue-resouce

二、get方式

1、get获取一个普通文本数据:

window.οnlοad=function(){

new Vue({

el:'body',

data:{

},

methods:{

get:function(){

this.$http.get('a.txt').then(function(res){

alert(res.status);//成功

alert(res.data);

},function(res){

alert(res.status);//失败返回

alert(res.data);

});

}

}

});

};

2、get给服务发送数据:

window.οnlοad=function(){

new Vue({

el:'body',

data:{

},

methods:{

get:function(){

this.$http.get('get.php',{

a:1,

b:2

}).then(function(res){

alert(res.data);

},function(res){

alert(res.status);

});

}

}

});

};

三、post方式

window.οnlοad=function(){

new Vue({

el:'body',

data:{

},

methods:{

get:function(){

this.$http.post('post.php',{

a:1,

b:20

},{

emulateJSON:true

}).then(function(res){

alert(res.data);

},function(res){

alert(res.status);

});

}

}

});

};

四、jsonp方式

获取百度接口

查看响应数据

jsonp请求百度接口

window.οnlοad=function(){

new Vue({

el:'body',

data:{

},

methods:{

get:function(){

this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{

wd:'a'

},{

jsonp:'cb'//回调函数名称

}).then(function(res){

alert(res.data.s);

},function(res){

alert(res.status);

});

}

}

});

};

jsonp请求360接口

window.οnlοad=function(){

new Vue({

el:'body',

data:{

},

methods:{

get:function(){

this.$http.jsonp('https://sug.so.360.cn/suggest',{

word:'a'

}).then(function(res){

alert(res.data.s);

},function(res){

alert(res.status);

});

}

}

});

};

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试上述代码运行效果。

希望本文所述对大家vue.js程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值