[英]How to send data to routes use get method on the vue.js 2?
我的ajax是這樣的:
<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>
我的路線是這樣的:
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;
});
執行后,我在控制台上檢查結果= null,而我將其放在: dd(Input::get('page'));
是否顯示發送誰的頁面
我該如何解決?
嘗試更改行:
dd(Input::get('page'));
至:
return Input::get('page');
dd
方法將轉儲var
值並暫停執行。 因此,輸出將不是格式正確的json。 可能是一些原始html取代了您轉儲的值。
這是vue js代碼的完整示例
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)
}
});
}
}
})
有路線
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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.