gpt4 book ai didi

javascript - 如何将错误消息关联到 json 响应中的 'error' 键

转载 作者:行者123 更新时间:2023-11-30 06:38:23 27 4
gpt4 key购买 nike

我试图显示上传文件(文件名、大小)可能发生的错误。我正在使用 an adapted version of Jquery Fiel Upload for Ruby on Rails

我需要将我的错误消息(验证)关联到我的 json 响应中的“错误”键。

我想我必须更改 javascript 才能使其工作。

json:

render json: {error: @upload.errors.full_messages}, status: :unprocessable_entity

验证:

 validates_uniqueness_of :upload_file_name, :message => "File with this name is already in the database"

validates :upload_file_size, :inclusion => {:in =>10.megabytes..20.megabytes}, :message =>"Too big or too small"

我用来上传文件的 Javascript:

<script type="text/javascript" charset="utf-8">
$(function () {
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload();
//
// Load existing files:
$.getJSON($('#fileupload').prop('action'), function (files) {
var fu = $('#fileupload').data('fileupload'),
template;
fu._adjustMaxNumberOfFiles(-files.length);
console.log(files);
template = fu._renderDownload(files)
.appendTo($('#fileupload .files'));
// Force reflow:
fu._reflow = fu._transition && template.length &&
template[0].offsetWidth;
template.addClass('in');
$('#loading').remove();
});

});
</script>

我也试过那些东西:json: => [{:error=> @upload.errors.full_messages }] }使用它仅显示验证消息,但我还需要状态:

最佳答案

解决方法如下:

json:

format.json{ render json: {error: @upload.errors.full_messages}, :status =>422}

javascript(主要代码截取)

$('#fileupload').fileupload({       


error: function(xhr){
var errors = $.parseJSON(xhr.responseText).error
alert(errors)
}
});

关于javascript - 如何将错误消息关联到 json 响应中的 'error' 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109333/

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