我正在尝试附加URL,但它生成的URL不是预期的。下面是我测试过的代码及其结果。
因为我使用本地服务器来测试我的系统,所以所需的请求URL是http://127.0.0.1:8000/api/posts
。我将在不久的将来将此系统部署到远程服务器,因此我不能像现在这样使用请求URL。基于下面的代码,我试图做的是获取当前主机名并将其附加到路由URL,但它产生了奇怪的URL。如何解决这个问题?
组件
created() {
var test = window.location.hostname + '/api/posts';
this.axios.get(test).then(response => {
this.posts = response.data.data;
});
路由Api (api.php)
Route::get('/posts', 'PostController@index');
发布于 2019-06-09 14:18:43
如果不想配置基本URL,只需在axios请求中使用绝对URL:
this.$axios.get('/apiposts')
其中带前缀的/
是重要的部分。
发布于 2019-06-09 14:01:09
您可能不需要设置baseURL。你有没有试过定义baseURL?例如:
axios.get(`${process.env.HOST}:${PORT}/api/categories`)
在您的:/src/main.js
中添加以下代码
const baseURL = 'http://localhost:8080';
if (typeof baseURL !== 'undefined') {
Vue.axios.defaults.baseURL = baseURL;
}
请在此处查看解决方案Set baseURL from .env in VueAxios
我认为在您的应用程序中,baseURL设置为http://127.0.0.1:8000
(默认),您可以在var test = window.location.hostname + '/api/posts';
这一行中将主机附加到此url。试着不用这个。
https://stackoverflow.com/questions/56512318
复制相似问题