简体   繁体   English

如何在vue.js 2上使用get方法将数据发送到路由?

[英]How to send data to routes use get method on the vue.js 2?

My ajax is like this : 我的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>

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')); 执行后,我在控制台上检查结果= null,而我将其放在: 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. dd方法将转储var值并暂停执行。 So the output won't be a properly formatted json. 因此,输出将不是格式正确的json。 It will probably be some raw html displaing the value you dumped. 可能是一些原始html取代了您转储的值。

This is full example of vue js code 这是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)
              }
           });
        }
     }
 })

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM