vue-cli跨域 vue使用axios请求接口怎么操作?
vue-cli中使用axios跨域请求,结果报错了,那么应该怎么在vue-cli中使用axios跨域请求呢?...
vue-cli中使用axios跨域请求,结果报错了,那么应该怎么在vue-cli中使用axios跨域请求呢?
展开
1个回答
展开全部
全局使用Axios
首先,在自己建的公用方法的文件中new一个新的HttpUtil.js文件。以下为HttpUtil.js的内容:
var axios = require('axios')// 配置项目根如路径var root = 'http://localhost:8090/manage'// axios请求function httpApi (method, url, params) { return new Promise((resolve, reject) => { axios({ method: method, url: url, data: method === 'POST' || method === 'PUT' ? params : null, params: method === 'GET' || method === 'DELETE' ? params : null, baseURL: root, withCredentials: false }).then(
(response) => {
resolve(response)
}
).catch(
(error) => {
reject(error)
}
)
})
}// 返回在vue模板中的调用接口export default { get: function (url, params) { return httpApi('GET', url, params)
}, post: function (url, params) { return httpApi('POST', url, params)
}, put: function (url, params) { return httpApi('PUT', url, params)
}, delete: function (url, params) { return httpApi('DELETE', url, params)
}
}
首先,在自己建的公用方法的文件中new一个新的HttpUtil.js文件。以下为HttpUtil.js的内容:
var axios = require('axios')// 配置项目根如路径var root = 'http://localhost:8090/manage'// axios请求function httpApi (method, url, params) { return new Promise((resolve, reject) => { axios({ method: method, url: url, data: method === 'POST' || method === 'PUT' ? params : null, params: method === 'GET' || method === 'DELETE' ? params : null, baseURL: root, withCredentials: false }).then(
(response) => {
resolve(response)
}
).catch(
(error) => {
reject(error)
}
)
})
}// 返回在vue模板中的调用接口export default { get: function (url, params) { return httpApi('GET', url, params)
}, post: function (url, params) { return httpApi('POST', url, params)
}, put: function (url, params) { return httpApi('PUT', url, params)
}, delete: function (url, params) { return httpApi('DELETE', url, params)
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询