码迷,mamicode.com
首页 > 移动开发 > 详细

vue-axios

时间:2019-08-14 12:52:19      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:his   efault   instance   err   ring   对象序列化   app   name   save   

//安装axios
npm install vue-axios --save
//最好安装qs来配合axios使用,qs可将对象序列化成jsonstr,或将jsonstr反序列化成对象
npm install qs.js --save

在入口文件main.js中

//引入axios 和 qs
import axios from ‘axios‘;
import Qs from ‘qs‘;
//配置axios允许跨域请求(服务器端也需要另外的配置)
axios.defaults.withCredentials=true;
axios.defaults.crossDomain=true;
axios.defaults.headers.post[‘Content-Type‘] = ‘application/x-www-form-urlencoded‘;
//给Vue函数添加一个原型属性axios 指向Axios,vue实例或组件中不用再去重复引用Axios,qs同理,vue实例中直接通过this.axios可进行调用
Vue.prototype.axios = axios;
Vue.prototype.qs = Qs;

调用

var postUrl = ‘xxxxx‘;
var instance = this.axios.create(
{
        headers: {‘content-type‘: ‘application/x-www-form-urlencoded‘}
});
instance.post(postUrl,
    this.qs.stringify(
    {
        username: ‘xx‘,
        password: ‘xx‘
    }
    )).then(function (response) 
    {
        alert("rsp_"+JSON.stringify(response.data));
    }).catch(function (error) 
    {
          alert("err_"+error);
    }
);

 

vue-axios

标签:his   efault   instance   err   ring   对象序列化   app   name   save   

原文地址:https://www.cnblogs.com/IsSshuai/p/11351007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!