【问题标题】:Vuejs and Laravel sending data not workingVuejs 和 Laravel 发送数据不起作用
【发布时间】:2015-11-09 09:52:53
【问题描述】:

我有一个 vue.js 脚本,它获取一个 csv,将其发送到我的服务器端代码,该代码将其分解为一个数组并将其发回。

现在,我将这些数据保存到一个数组中,以便客户端可以编辑任何数据并进行操作等。这是执行此操作的代码:

//hide the upload box, and display new message
$('#upload').slideUp();
$('#message').html('Cheers, Just give us a sec to decode the file...');

//get the file and save it into a new File Object ready to send
var files = $('input#csvUpload').prop('files');
var csv_file = new FormData();
csv_file.append('csv_file', files[0]);


//send to a controller that will read this file and return a JSON string!
this.$http.post('uploadCSV', csv_file, function(data){
   //the data is the array returned from the controller,
   //this function is just the call back

   //now for the rows
   this.rows    = data.rows;

   console.log(this.rows);

   //update the message with some new instructions
   $('#message').html("Done, now remove the cards you don't want to process");

   //and show our table
   $('#table').slideDown();

    });

现在,一切正常。没有错。令人困惑的部分是,在最终用户完成更改后,我需要将该数据发送到将处理该数据的控制器。 但问题是当我发送数据时,当它到达控制器时,laravel 似乎无法找到它。

发送数据的代码:

  this.$http.post('makePayment', this.rows, function(data){
     this.processed = data;
     console.log(data);
   });

控制器代码:

$array = $request->all();
return $array;
exit();

我有一种感觉,它在盯着我的脸,但这真的让我很难过,上面的图片显示了 this.rows 对象中的内容。

提前感谢您的帮助!

【问题讨论】:

    标签: javascript laravel-5.1 vue.js


    【解决方案1】:

    好吧,我知道它在盯着我看。所以解决方案非常简单。发生的事情是我发送的是一个数组而不是 json 字符串......应该没关系吧?很好。所以简单的解决方案是在将数据发送到我的控制器之前,我需要将 array 转换为 Json 格式...

    所以这是发送我的数据的更新代码:

    var newJson = JSON.stringify(this.rows);
    this.$http.post('makePayment', newJson, function(data){
          this.processed = data;
          console.log(data);
    });
    

    【讨论】:

      猜你喜欢
      • 2022-10-25
      • 1970-01-01
      • 2018-10-10
      • 2017-02-17
      • 1970-01-01
      • 2016-03-05
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多