vue中的请求(前后台交互)

1:请求依赖

​ 在vue中请求依赖于vue-resource.js

2:vue的get请求实例

<html>
	<head>
    	<title></title>
        <script src="vue.js"></script>
        <script src="vue-resource.js"></script>
    </head>
<body>
  
    <div id="app">  
     
    </div>    
    <script>
    	var app = new VUE({
            el:'#app',
            data:{
               list:[
                   {id:1,name="123"},
                   {id:2,name="123"}
               ]
            },
            methods:{
                add(){
                    this.$http.get('请求地址',参数).then(function (result){
                        console.log(result);
                    });
                }
            }            
        });	
    </script>            
    </body>
</html>

3:vue的post请求实例

<html>
	<head>
    	<title></title>
        <script src="vue.js"></script>
    </head>
<body>
  
    <div id="app">  
     
    </div>    
    <script>
    	var app = new VUE({
            el:'#app',
            data:{
               list:[
                   {id:1,name="123"},
                   {id:2,name="123"}
               ]
            },
            methods:{
                add(){
                    //手动发起的post请求,默认没有表单格式,
                    //所以设置  emulateJSON:true 为表单格式
                    this.$http.post('请求地址',参数,{emulateJSON:true} ).then(
                        function (result){
                            console.log(result.boby);
                        });
                }
            }            
        });	
    </script>            
    </body>
</html>

4:初始化请求数据

<html>
	<head>
    	<title></title>
        <script src="vue.js"></script>
    </head>
<body>
  
    <div id="app">  
     
    </div>    
    <script>
    	var app = new VUE({
            el:'#app',
            data:{
               list:[
                   {id:1,name="123"},
                   {id:2,name="123"}
               ]
            },
            created(){//在页面加载的时候的初始化方法
			  this.query();
			},
            methods:{
                query(){
                    //手动发起的post请求,默认没有表单格式,
                    //所以设置  emulateJSON:true 为表单格式
                    this.$http.post('请求地址',参数,{emulateJSON:true} ).then(
                        function (result){
                            console.log(result.boby);
                     });
                }
            }            
        });	
    </script>            
    </body>
</html>

5:vue中上传文件

<html>
	<head>
    	<title></title>
        <script src="vue.js"></script>
        <script src="vue-resource.js"></script>
    </head>
<body>
  
    <div id="app">  
     
    </div>    
    <script>
    	var app = new VUE({
            el:'#app',
            data:{
            },
            methods:{
                query(){
                    var upload = new FormData();
                    upload.append("upfile",文件对象);
                    upload.append("name",文件名称);
                    
                    this.$http.post('路径',upload).then(function(result){
                        console.log(result);
                    });
                }
            }            
        });	
    </script>            
    </body>
</html>

6:以对象的形式传递参数

<html>
	<head>
    	<title></title>
        
        <script src="vue.js"></script>
        <script src="vue-resource.js"></script>
        
    </head>
<body>
  
    <div id="app">  
     
    </div>    
    <script>
    	var app = new VUE({
            el:'#app',
            data:{
               order:{
                   oid:'',
                   oname:''
               },
               orderService:{
                   osid:'',
                   osname:''
               }
                
            },
            methods:{
                query(){
                    var _this = this;
                    var json = {  //将对个对象封装成一个对象
                        this.order, 
                        this.orderService
                    }
                    //将对象数据转换为JSON串
                    var data = JSON.stringify(json);
                    
                    this.$http.post(
                    	"地址",
                        //发送JSON串数据
                        {"data":data}, 
                         //所以设置  emulateJSON:true 为表单格式
                        {emulateJSON:true}
                    ).then(result=>{
                        //返回值操作
                    });
                }
            }            
        });	
    </script>            
    </body>
</html>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值