Vuejs和Laravel发送数据不起作用

时间:2015-11-09 09:52:53

标签: javascript laravel-5.1 vue.js

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

现在,我将该数据保存到数组中,以便客户端可以编辑任何数据并操作ect。这是代码的代码:

//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();

    });

this is the data that has been returned

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

发送数据的代码:

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

enter image description here

控制器代码:

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

enter image description here

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

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

好的,知道它正盯着我的脸。所以解决方案非常简单。发生的事情是我发送了一个数组而不是json字符串...不应该对吗?它确实如此。所以简单的解决方案是在将数据发送到我的控制器之前,我需要将数组转换为 Json 格式......

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

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