axios总结

GET数据格式
        get_be_data(){
                    axios({// (请求跨域请求线上数据 - 卖座)
                        url: 'https://m.maizuo.com/gateway',
                        headers: {//设置请求头用于跨域请求
                            'X-Client-Info': 
                            '{"a":"3000","ch":"1002","v":"5.0.4","e":"154549400038873748996477"}',
                            'X-Host': 'mall.film-ticket.film.list'
                        },
                        params: {//相当于ajax的data
                            cityId: 330100,
                            pageNum: 1,
                            pageSize: 10,
                            type: 1,
                            k: 7675918
                        }
                    })//axios相当于vue里面封装了promise对象
                        .then(res => console.log(res))
                        .catch(error => console.log(error))
                    }
POST数据请求格式
        postdata() {
                    var params = new URLSearchParams() //得到params对象,用来接收参数
                    // params.append( key, value )  key就是参数名,value就是参数值
                    params.append('a', 2)
                    params.append('b', 2)
                    axios({
                        url: 'http://localhost/Drafts/post.php',
                        method: 'post',
                        headers: {
                            'Content-Type': "application/x-www-form-urlencoded" 
                            //请求头设置为表单提交的请求头
                        },
                        data: params
                    })
                        .then(res => console.log(res))
                        .catch(error => console.log(error))
                    }

注意:Query String Parameters查询字符串参数用于get请求
Form Data 用于post请求

猜你喜欢

转载自blog.csdn.net/HelloWord182/article/details/93238461
今日推荐