jQuery File Upload,指定FormData

编程入门 行业动态 更新时间:2024-10-11 21:27:32
本文介绍了jQuery File Upload,指定FormData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用以下jQuery File Upload插件:

I'm working to use the following jQuery File Upload plugin:

github/blueimp/jQuery-File-Upload/wiki/Options

我需要特定的额外formdata,它说有一个选项,但是我遇到了JS错误未捕获的SyntaxError:意外的标识符",并且没有FormData示例,这使得工作变得很困难.

I need to specific extra formdata which it says there is an option for, but I'm getting a JS error "Uncaught SyntaxError: Unexpected identifier" and there are no FormData examples, which is making it hard to get to work.

这是我所拥有的:

$(function () { $('.upload').fileUploadUI({ uploadTable: $('.upload_files'), downloadTable: $('.download_files'), buildUploadRow: function (files, index) { var file = files[index]; return $( '<tr>' + '<td>' + file.name + '<\/td>' + '<td class="file_upload_progress"><div><\/div><\/td>' + '<td class="file_upload_cancel">' + '<div class="ui-state-default ui-corner-all ui-state-hover" title="Cancel">' + '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' + '<\/div>' + '<\/td>' + '<\/tr>' ); }, buildDownloadRow: function (file) { return $( '<tr><td>' + file.name + ' ' + file.type + ' ' + file.size + '<\/td><\/tr>' ); }, formData: [ { name: '_http_accept' value: 'application/javascript' }, { name: '<%= session_key_name %>' value: encodeURIComponent('<%= u cookies[session_key_name] %>'), }, { name: 'authenticity_token' value: encodeURIComponent('<%= u form_authenticity_token %>') } ] }); });

推荐答案

您在formData的正确位置没有逗号,我想您希望它像这样:

You don't have commas in the right places in your formData, I think you want it to be like this:

formData: [ { name: '_http_accept', value: 'application/javascript' }, { name: '<%= session_key_name %>', value: encodeURIComponent('<%= u cookies[session_key_name] %>') }, { name: 'authenticity_token', value: encodeURIComponent('<%= u form_authenticity_token %>') } ]

请注意,name: ...部分后面有逗号,而value: ...部分后面没有逗号.

Note that there are commas after the name: ... parts but not the value: ... parts.

此外,我不认为encodeURIComponent()是这里合适的转义/编码机制,并且<%= u ...已经URI编码.您需要做的就是确保字符串不包含未转义的单引号,这样类似的事情可能会起作用(假设此JavaScript正在通过ERB):

Also, I don't think encodeURIComponent() is the appropriate escaping/encoding mechanism here and <%= u ... already URI encodes. All you need to do is make sure the string doesn't contain an unescaped single quote so something more like this would probably work (assuming that this JavaScript is going through ERB):

value: '<%= cookies[session_key_name].gsub(/'/, "\'") %>'

适当的编码应由插件处理,并且几乎可以肯定它正在执行POST,因此URL编码甚至不适用.

The appropriate encoding should be handled by the plugin and it is almost certainly doing a POST anyway so URL encoding doesn't even apply.

此外,您不需要在JavaScript字符串中转义斜线,因为它们不是特殊的,所以您可以在'</td>'所在的地方说'<\/td>'.

Also, you don't need to escape slashes in JavaScript strings, they're not special so you can just say '</td>' where you say '<\/td>'.

我对jQuery-File-Upload一无所知,但修复逗号至少应该使您摆脱眼前的问题(以及新的和更有趣的问题!).

I don't know anything about jQuery-File-Upload but fixing your commas should at least get you past your immediate problem (and on to new and more interesting problems!).

更多推荐

jQuery File Upload,指定FormData

本文发布于:2023-10-10 22:23:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:File   jQuery   FormData   Upload

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!