Just use '/exp/laravel-vuejs/api/tips' - if the address starts with a forward slash it will be relative to the domain.
Aug 29, 2016
11
Level 1
Laravel 5.3 Vue.js http get URL
Hi guys,
Here is my vue.js code
el: '#tips',
ready: function(){
this.fetchTips();
},
methods: {
fetchTips: function(){
this.$http.get('/api/tips', function(tips){
this.$set('tips', tips)
});
}
}
});
Now, vue calls this "http://localhost:8080/api/tips" while it should call "http://localhost:8080/exp/laravel-vuejs/api/tips"
How do I fix this without having to put the full localhost:8080 etc... into the vue code?
Thanks
Level 1
Not sure it's the best solution but at least I got it to work.
In my laravel view:
<input type="hidden" value="{{ url('/') }}" v-model="baseUrl" />
Vue:
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');
new Vue({
el: '#tips',
data:{
newTip: { title: '', content: ''},
submitted: false,
baseUrl: '',
tips: []
},
ready: function(){
this.fetchTips();
},
methods:{
fetchTips: function(){
this.$http.get(this.baseUrl + '/api/tips', function(tips) {
this.$set('tips', tips);
});
},
addTip: function(e){
e.preventDefault();
var tip = this.newTip;
tip.content = $('#tipRteInstance').html();
this.tips.push(tip);
this.newTip = { title: '', content: '' }; $('#tipRteInstance').html('');
//Vue.http.options.emulateJSON = true;
this.$http.post(this.baseUrl + '/api/tips', tip);
this.submitted = true;
}
}
});
2 likes
Please or to participate in this conversation.