gpt4 book ai didi

javascript - Angular 无法访问返回的数据

转载 作者:行者123 更新时间:2023-11-28 01:26:04 25 4
gpt4 key购买 nike

在我的 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 接受文件并处理它。我尝试使用 render json: {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;
});

最佳答案

不确定,但我看到的成功的结构看起来像

.success(函数(数据、状态、标题、配置){
$scope.selector.tabledata = 数据;
$scope.uploaded = true;
});

关于javascript - Angular 无法访问返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22667319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com