---
title: "axios 跨域问题解决 "
author: "e_e"
date: "2018-10-18T09:27:24Z"
tags: []
categories: ["Web前端"]
slug: "axios"
draft: false
lastmod: "2018-10-18T09:27:43Z"
id: 2432353
url: "https://e-e.iteye.com/blog/2432353"
---

1.main.js 引入axios

```
import axios from 'axios';
```

```
Vue.prototype.$http = axios;
```

 

2\. 发起post请求

```
methods: submitForm:function(){
        this.$http.get("/user/find?id=1").then(r=> console.log(r.data));
this.$http.defaults.headers.post['Content-Type']='application/json;charse=UTF-8';
this.$http.post(this.url, JSON.stringify(this.user)).then((response) => {
            console.log(response.status);
console.log(response.statusText);
}, (response) => {
        });
}
}
```

 

3\. index.js 里配置代理

    请求的目标地址是http://localhost:80/user/add

```
proxyTable: {
    '/user': {
        target: 'http://localhost:80',
changeOrigin: true,
pathRewrite:{
            '^/user/':'/'
}
    }
},
```