Check your headers. Make sure you set the headers the same as postman.
ajax request returns null but works in postman
I am trying to make my first ajax/api request in laravel 5.3 using vue. The api call works fine in postman but gives me null from the vue request. This is my api defenition:
/**
* Get all students with a given name
*/
public function students(Request $request) {
$namePart = $request->namepart ? $request->namepart : '';
// get all names like namepart
$students = Student::where('name', 'like', '%' . $namePart . '%')->get();
return response()->json($students);
}
and the route in routes/api.php
Route::get('/students', 'ApiController@students');
In postman I call this using my (local) url: http://admo/api/students?namepart=spe This returns an array of json objects (i removed the data values)
[
{
"id": 8,
"name": "",
"address": "",
"zipcode": "",
"city": "",
"email": "",
"date_of_birth": "",
"permission_auto_banktransfer": "",
"bankaccount_name": "",
"bankaccount_number": "",
"status": "",
"created_at": null,
"updated_at": null
}
]
This is my vue code:
new Vue({
el: '#studentlist',
data: {
students: []
},
mounted: function() {
this.fetchStudents();
},
methods: {
fetchStudents: function() {
this.$http.get('api/students?namepart=spe', function(response) {
this.students = response;
})
}
}
});
in firebug I see the request but it returns null
Does anyone see the problem here?
-- Update:
if i use jQuery $.ajax instead of this.$http.get it works alright. Something with vue-resource perhaps?
Change to axios and either set Vue.prototype.$http=axios; or just use as documented. I find it works much better than vue resource.
Please or to participate in this conversation.