Angular无法访问返回的数据

时间:2014-03-26 16:28:07

标签: javascript ruby-on-rails angularjs

在我的Angular / Rails应用中,我使用 angular-file-upload gem上传电子表格。 https://github.com/danialfarid/angular-file-upload

我将电子表格传递给Rails后端,它处理数据并返回JSON数组。

传递文件的Angular代码是:

$scope.submitForm = function () {
  console.log('submit upload2');
  return $scope.upload = $upload.upload({
    url: '/api/batches/spreadsheet_upload.json',
    data: {
      id: 99,
      assembly: "xy"
    },
    file: $scope.file
  }).success(data, status, headers, config)(function () {
      debugger
    $scope.selector.tabledata = data;
    $scope.uploaded = true;
  });
};

Rails接受该文件并对其进行处理。我试图用渲染json:{data:spreadsheet_data}

返回它
def spreadsheet_upload
  Rails.logger.debug("here params: #{params}")
  spreadsheet = nil
  case File.extname(params[:file].original_filename)
    when '.csv' then spreadsheet = Roo::CSV.new(params[:file].path)
    when '.xls' then spreadsheet = Roo::Excel.new(params[:file].path, nil, :ignore)
    when '.xlsx' then spreadsheet = Roo::Excelx.new(params[:file].path, nil, :ignore)
    else raise "Unknown file type: #{file.original_filename}"
  end

  spreadsheet_header = spreadsheet.row(1)
  spreadsheet_data = []

  (2..spreadsheet.last_row).each do |i|
    row = Hash[[spreadsheet_header, spreadsheet.row(i)].transpose]
    spreadsheet_data.push(row)
  end
  render json: {data: spreadsheet_data}
end

Chrome中的网络标签显示我已从Rails服务器返回数据,但Angular似乎并不知道如何处理它。

ReferenceError: data is not defined
    at Object.$scope.submitForm (http://0.0.0.0:3000/assets/controllers/DesignViewCtrl.js?body=1:203:18)
    at http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:1936:30
    at http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:4127:15
    at Object.e.$eval (http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:2432:24)
    at Object.e.$apply (http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:2439:40)
    at HTMLFormElement.<anonymous> (http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:4126:15)
    at HTMLFormElement.jQuery.event.dispatch (http://0.0.0.0:3000/assets/jquery.js?body=1:2940:98)
    at HTMLFormElement.elemData.handle (http://0.0.0.0:3000/assets/jquery.js?body=1:2750:123) 

网络标签中的返回数据

{"data":[{"chrom":4.0,"chrom_start":55593607.0,"chrom_end":55593607.0},   {"chrom":"4","chrom_start":55593609.0,"chrom_end":55593617.0},{"chrom":"6","chrom_start":133105152.0,"chrom_end":133105152.0}]}

我也尝试过更改回复,但这也没有用。

  }).success(resp)(function () {
    $scope.selector.tabledata = resp.data;
    $scope.uploaded = true;
  });

1 个答案:

答案 0 :(得分:1)

不确定,但我见过的success上的结构看起来像是

.success(function(data, status, headers, config) { $scope.selector.tabledata = data; $scope.uploaded = true; });