An article with you to understand axios network interaction -Vue

Source: Chuzhou SEO

1

** axios What is it? ** understand, and to use it for both ways axios sending the request to understand what, how and to axios cross-domain problem is solved.

For axios network interaction, to use axios the same time, first you have to understand what it is, how to use is. Axios said interactive network, i.e. the transmission request, said earlier two methods to transmit a GET request, a POST request to two.

Axios solve cross-domain problems.

2

To use axios, it is not to understand it, explain it. axios Promise is based HTTP library, can be used in the browser and node environment, the application sends a request to the server while Ajax obtain the corresponding server-side HTTP request response library.

Why do we use it? What are its benefits. Can be used alone to support Promise API, to solve the problem of JavaScript "callback hell", can send Cookie, intercepting HTTP authentication, concurrent requests, requests and responses, cancellation requests, etc., automatically convert json data for restful api scene.

Ajax vue by acquiring from the data server, separate front and rear ends, the rear end api request interface is responsible for providing front end server data acquisition Ajax. Api server interface is generally used restful api.

Using Ajax to get data in two ways:

JQuery Ajax method XMLHTTPRequest object provided

3

Axios understand what is? What to do, how to use it? Use axios, its use is widespread, can be used in vue cli can also be used in non-Vue cli applications. The use of two types, a, in use axios vue CLI application, two, used in vue file.

In the first case, in vue cli application, then you must create vue cli application, and then install axios, then configure axios, ready to use.

Installation axios of:

Use Npm or yarn, or using the installation vue axios.

After the installation is complete, plug in both the plugins directory, there are axios.js file, and then introduced into the plug-in plugins plug-in entry file.

// main.js
import './plugins/axios'

Use axios can get network data:

// 实例
created: function(){ const app = this; axios.get('接口').then(res=>{ app.users = res.data.data; }); } 

Axios in vue file, and the file is introduced vue.js axios.js file, send a request using Ajax axios.

script(导入vue.js)
script(导入axios.js)

created: function(){
 // 创建vue实例,axios获取数据 axios.get('接口').then(function(res){ console.log(res.data); }).catch(function(error){ console.log(error); }); } 

send request

General points: sending a GET request, a POST request, and:

GET pass, there are two writing request parameters, one is directly in the url additional parameters, one is used to add attribute parmas GET parameter.

// url
axios.get('...?name=da').then(function(response){ console.log(response.data); this.user = response.data.data; }).catch(function(error){ console.log(error); }); // parmas属性 axios.get('...', { params: { name: 'dada' } }) .then(function(response){ console.log(response.data); this.users = response.data.data; }).catch(function(error){ console.log(error); }); 

Send a post request:

button(@click="addage")
addage: function(){ const app = this; let params = new URLSearchParams(); params.append('age1', 1); axios.post('url...', params).then(function(res){ console.log(res.data); }); } 

json format:

module.exports = function(app){
 // 配置json app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); }; adduser: function(){ consot app = this; axios.post('接口',{ name: 'dada', age: 1 }).then(function(res){ console.log(res.data); }); } if(res.data.status){ // 数据插入成功 app.user.push(res.data.data); } 

4

Cross-domain issues

What is cross-domain problem is access to resources in other domains problem occurs while accessing the same domain will not be a problem, how to solve this problem, this problem is called cross-domain issues.

Solutions used:

The first is to configure the framework of cross-border visits by vue, the second thing by the server, modify the node program to implement cross-domain issues.

In vue framework vue.config.js, configure the proxy server.

module.exports = {
devServer: {
proxy: '域名'
}
};
// 代理服务器处理 created:function(){ const app = this; axios.get('/users').then(res=>{ app.users=res.data.data; }); } 

Set up multiple proxy cross-domain requests, but there are problems in a production environment, or there will be cross-domain issues.

module.exports = {
devServer: {
// 多个代理请求
proxy: {
 "/api":{ target: 'http://...', pathRewrite:{ "^/api": } } }}; created: function(){ const app = this; axios.get('api/users').then(res=>{ app.users=res.data.data; }); } 

Server-side support for cross-domain access

express server, cors open, cross-domain resources sharing, before opening, to support cross-domain cors install module, introducing const cors = require ( 'cors').

Then add the middleware: app.use (cors ()) can be.

❤️ Do not forget to leave your footprints learning [+ collection point Like Comments +]

Guess you like

Origin www.cnblogs.com/1994july/p/12078737.html