vue框架与后台之间的http交互

1. 引入vue.js, vue-resource.js;

2.这就可以开始交互了。

注意:一定要运行在服务器里面,否则输出的是php代码,而非返回值。

有三种交互方式:get、post、jsonp

get:会将请求的参数附在最后

post:不会显示在url中

get、post用来请求某个php文件的参数。

jsonp:JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题。其核心思想是利用JS标签里面的跨域特性进行跨域数据访问,在JS标签里面存在的是一个跨域的URL,实际执行的时候通过这个URL获得一段字符串,这段返回的字符串必须是一个合法的JS调用,通过EVAL这个字符串来完成对获得的数据的处理。

定义a.txt

内容是:Hello world!

1)get:

php接口:

 

 
  1. <?php

  2. $a=$_GET['a'];

  3. $b=$_GET['b'];

  4. echo $a+$b;

  5. ?>

 

vue:

 
  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>Ajax-get</title>

  6. <script src="vue.js"></script>

  7. <script src="vue-resource.js"></script>

  8. </head>

  9. <body>

  10. <div id="app">

  11. a= <input type="text" id="t1" v-model="t1">

  12. b= <input type="text" id="t2" v-model="t2">

  13. a+b=<span class="a">{{msg}}</span>

  14. <input type="button" value="submit" @click="get()">

  15. </div>

  16.  
  17. <script>

  18. new Vue({

  19. el:'body',

  20. data:{

  21. t1:'',

  22. t2:'',

  23. msg:''

  24. },

  25. methods:{

  26. get:function(){

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

  28. a:this.t1,

  29. b:this.t2

  30. }).then(function(res){

  31. this.msg=res.data;

  32. },function(res){

  33. alert(res.status);

  34. });

  35. }

  36. }

  37. });

  38. </script>

  39. </body>

  40. </html>

 

 

 

 

2)  post:

php接口:

 
  1. <?php

  2. $a=$_POST['a'];

  3. $b=$_POST['b'];

  4. echo $a-$b;

  5. ?>

 

vue:

 
  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>Ajax-post</title>

  6. <script src="vue.js"></script>

  7. <script src="vue-resource.js"></script>

  8. </head>

  9. <body>

  10. <div id="app">

  11. a= <input type="text" id="t1" v-model="t1">

  12. b=<input type="text" id="t2" v-model="t2">

  13. a-b=<span class="a">{{msg}}</span>

  14. <input type="button" value="submit" @click="post()">

  15. </div>

  16.  
  17. <script>

  18. new Vue({

  19. el:'body',

  20. data:{

  21. t1:'',

  22. t2:'',

  23. msg:''

  24. },

  25. methods:{

  26. post:function(){

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

  28. a:this.t1,

  29. b:this.t2

  30. },{

  31. emulateJSON:true

  32. }).then(function(res){

  33. this.msg=res.data;

  34. },function(res){

  35. alert(res.status);

  36. });

  37. }

  38. }

  39. });

  40. </script>

  41. </body>

  42. </html>

 

 

 

 

3)  JOSNP:

 
  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>Ajax</title>

  6. <script src="vue.js"></script>

  7. <script src="vue-resource.js"></script>

  8. </head>

  9. <body>

  10. <div id="app">

  11. <input type="text" id="t1" v-model="t1">

  12. <p class="a">{{msg1}}</p>

  13. <input type="button" value="submit" @click="get()">

  14. </div>

  15. <script>

  16. new Vue ({

  17. el:'#app',

  18. data:{

  19. t1:'',

  20. msg1:''

  21. },

  22. methods:{

  23. get:function(){

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

  25. wd:this.t1

  26. },{

  27. jsonp:'cb' //callback名字,默认名字就是"callback"

  28. }).then(function(res){

  29. this.msg1=res.data.s;

  30. },function(res){

  31. alert(res.status);

  32. });

  33. }

  34. }

  35. });

  36.  
  37. </script>

  38. </body>

  39. </html>

 

 

最传统的写法:

 
  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>Ajax</title>

  6. <script src="vue.js"></script>

  7. <script src="vue-resource.js"></script>

  8. </head>

  9. <body>

  10. <div id="app">

  11. <input type="text" id="t1" v-model="t1">

  12. <p class="a">{{msg1}}</p>

  13. <input type="button" value="submit" @click="get()">

  14. </div>

  15. <script>

  16. new Vue ({

  17. el:'#app',

  18. data:{

  19. t1:'a',

  20. msg1:'b'

  21. },

  22. methods:{

  23. get:function(){

  24. this.$http({

  25. url:'post.php'

  26. data:给后台提交数据,

  27. method:'get'//也可以是/post/jsonp

  28. //如果是jsonp,那么还要加一行:jsonp:'cb' //cbName

  29. }).then(function(res){

  30. this.msg1=res.data.s;

  31. },function(res){

  32. alert(res.status);

  33. });

  34. }

  35. }

  36. });

  37.  
  38. </script>

  39. </body>

  40. </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值