【问题标题】:How to send data to routes use get method on the vue.js 2?如何使用 vue.js 2 上的 get 方法将数据发送到路由?
【发布时间】:2017-02-12 01:36:14
【问题描述】:

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

是否应该显示发送者的页面

我该如何解决?

【问题讨论】:

    标签: javascript laravel vue.js laravel-5.3 vuejs2


    【解决方案1】:

    换行试试:

    dd(Input::get('page'));
    

    到:

    return Input::get('page');
    

    dd 方法将转储 var 值并停止执行。所以输出不会是格式正确的 json。它可能是一些原始的 html 来代替你转储的值。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2022-08-09
        • 1970-01-01
        • 1970-01-01
        • 2020-07-10
        • 2011-07-28
        • 1970-01-01
        • 2018-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多