My ajax is like this :
<script>
new Vue({
...
methods: {
fetchItems: function (page) {
var data = {page: page};
this.$http.get('api/items', data).then(function (response) {
console.log(JSON.stringify(response))
this.$set(this, 'items', response.data.data.data);
this.$set(this, 'pagination', response.data.pagination);
}, function (error) {
// handle error
});
},
...
}
});
</script>
My routes is like this :
Route::get('/api/items/', function () {
dd(Input::get('page'));
$results = \App\Post::latest()->paginate(7);
$response = [
'pagination' => [
'total' => $results->total(),
'per_page' => $results->perPage(),
'current_page' => $results->currentPage(),
'last_page' => $results->lastPage(),
'from' => $results->firstItem(),
'to' => $results->lastItem()
],
'data' => $results
];
return $response;
});
When executed, I check on the console, the result = null, whereas I had put this : dd(Input::get('page'));
Should it display page who sended
How can I solve it?
Try changing the line:
dd(Input::get('page'));
to:
return Input::get('page');
The dd
method will dump a var
value and halt the execution. So the output won't be a properly formatted json. It will probably be some raw html displaing the value you dumped.
This is full example of vue js code
var app = new Vue({
el: '#invoice',
data: {
form: new FormData(),
errors: {},
tax: 0
},
methods: {
random: function () {
this.form.serial = Math.floor(Math.random() * 1000000) + 1;
},
sortBy: function() {
if(this.form.tax_id) {
var url = '{{route('invoice.get_tax', null)}}';
this.$http.get(url)
.then(function(response){
if(response.data) {
console.log(response.data)
}
});
}
}
})
There is the route
Route::get('/get_tax/{tax_id}', function($tax_id)
{
$tax = App\Tax::findOrFail($tax_id);
if ($tax) {
return response()
->json([
'rate' => $tax->rate,
'type' => $tax->type
], 200);
} else {
return response()
->json([
'rate' => 0,
'type' => 1
], 200);
}
})->name('invoice.get_tax');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.