【03】vue.js — http请求交互
来源:互联网 发布:淘宝会追缴极速退款么 编辑:程序博客网 时间:2024/06/03 18:15
请求静态资源
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-resource.js" ></script> <script> window.onload = function(){ new Vue({ el: '#box', data: { }, methods: { get: function(){ this.$http.get('test.txt').then(function(successRes){ console.info(successRes.data); },function(errorRes){ }) } } }); } </script> </head> <body> <div id="box"> <input type="button" value="按钮" @click="get()" /> </div> </body></html>
text.txt文件中的内容是:这是测试文件
带参数请求后台
GET请求
- YouXinServlet.java文件内容
@WebServlet("/youxin")public class YouXinServlet extends HttpServlet { private static final long serialVersionUID = -8840959690122919168L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet请求"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost请求"); }}
- 前端静态页面html
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-resource.js" ></script> <script> window.onload = function(){ new Vue({ el: '#box', methods: { get: function(){ this.$http.get('youxin',{ a: 1, b: 2 }).then(function(successRes){ console.info(successRes); },function(errorRes){ }); } } }); } </script> </head> <body> <div id="box"> <input type="button" value="按钮" @click="get()" /> </div> </body></html>
POST请求
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-resource.js" ></script> <script> window.onload = function(){ new Vue({ el: '#box', 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); }); } } }); } </script> </head> <body> <div id="box"> <input type="button" value="按钮" @click="get()" /> </div> </body></html>
跨域请求 — JSONP(360)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-resource.js" ></script> <script> window.onload = function(){ new Vue({ el: '#box', methods: { get: function(){ this.$http.jsonp('https://sug.so.360.cn/suggest',{ word:'a' },{ emulateJSON: true }).then(function(successRes){ console.info(successRes.data); },function(errorRes){ }) } } }); } </script> </head> <body> <div id="box"> <input type="button" value="按钮" @click="get()" /> </div> </body></html>
跨域请求 — JSONP(百度)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-resource.js" ></script> <script> window.onload = function(){ new Vue({ el: '#box', methods: { get: function(){ this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{ wd: 'a' },{ //默认是使用callback,百度是cb jsonp: 'cb' }).then(function(successRes){ console.info(successRes.data); },function(errorRes){ }) } } }); } </script> </head> <body> <div id="box"> <input type="button" value="按钮" @click="get()" /> </div> </body></html>
阅读全文
0 0
- 【03】vue.js — http请求交互
- vue $http请求服务
- vue.js请求数据
- vue.js 2.0 axios交互
- Vue axios处理http请求
- Vue axios处理http请求
- Vue.js 请求后台数据
- js页面之间以及vue $http请求向后台传递参数中文乱码问题解决
- vue.js使用http-proxy-middleware解决跨域请求问题
- cocos2d - JS Http请求
- Vue-cli之http请求代理
- 【Vue】—Vue.js入门教程
- vue.js的ajax和jsonp请求
- Vue.js实现跨域请求
- vue.js中proxyTable 转发请求实现
- ajax请求+vue.js渲染+页面加载
- 浅谈Vue.js与后端API交互——axios的应用
- JS中使用HTTP请求
- MyISAM和InnoDB的索引在实现上的不同
- js for in循环获取对象属性名,属性值
- 使用Embulk从Oracle抽取数据到Trafodion
- poj 3690(二维哈希)
- java中的jre和jdk的区别
- 【03】vue.js — http请求交互
- centos7 安装psutil
- 【leetcode】88. Merge Sorted Array(Python & C++)
- 编译Android平台使用的FFmpeg库
- 直接插入排序(java版)
- Vuejs之路之--父子组件(未完待续)
- Elasticsearch系列(九)----使用Logstash-input-jdbc同步数据库中的数据到ES
- 开源 | 基于Python的人脸识别:识别准确率高达99.38%!
- SpringMVC开发入门讲义