[英]Remotipart file upload giving NaN as response code
I am using remotipart to upload a file with ajax in my rails app. 我正在使用remotipart在我的rails应用程序中使用ajax上传文件。 I am using the jquery-ujs Ajax "global" events
ajax:success
and ajax:error
in my javascript to trigger code on success or failure. 我在我的JavaScript中使用jquery-ujs Ajax“全局”事件
ajax:success
和ajax:error
触发成功或失败的代码。 The problem is that the error callback is always firing and never the success. 问题在于错误回调总是触发而从未成功。
I can see in the developer tools network tab that the request is returning a 200 OK status. 我可以在开发人员工具的“网络”标签中看到该请求返回200 OK状态。
This is my form where the remote upload is triggered. 这是触发远程上传的表单。
= form_tag({ action: 'import' }, html: { multipart: true }, class: 'csv_import_form', remote: true, data: { type: :json }) do
My controller processes the request as json and renders a json response: 我的控制器将请求处理为json并呈现json响应:
def import
render json: { some: 'sample json' }
end
and this is what the response body looks like as seen in developer tools: 这就是开发人员工具中所显示的响应主体:
<textarea data-type="application/json" data-status="200" data-statusText="OK">{"some":"sample json"}</textarea>
Remotipart wraps the response in a text area. Remotipart将响应包装在文本区域中。 Notice that it does include
data-status="200"
请注意,它确实包含
data-status="200"
Here is the javascript: 这是JavaScript:
$(function() {
$(document).on("ajax:success", ".csv_import_form", function(event, data, status, xhr) {
// does not get here
}).on("ajax:error", ".csv_import_form", function(event, xhr, status, error) {
console.log("status code: " + xhr.status);
console.log("json: " + xhr.responseText);
console.log("status text: " + xhr.statusText);
console.log("status: " + status);
console.log("error: " + error);
});
});
This is what I see output in the console: 这是我在控制台中看到的输出:
status code: NaN
json: {"some":"sample json"}
status text: OK
status: OK
error: OK
It seems the actual Textarea response is not being properly parsed. 似乎实际的Textarea响应未正确解析。 Even though it has data-status="200", it is being parsed as a NaN and thus invoking the error callback.
即使它具有data-status =“ 200”,它也被解析为NaN,从而调用错误回调。
Has anyone seen this? 有人看过吗?
Using: 使用:
rails 4.0.13
remotipart 1.2.1
jquery 1.7.2
Thanks 谢谢
It turned out that even though I upgraded the remotipart gem
to the latest version ( 1.2.1
at this time), the included javascript files had gotten out of date. 事实证明,即使我将
remotipart gem
升级到了最新版本(目前为1.2.1
),所包含的javascript文件也已过时。
To fix I deleted: 要修复,我删除了:
app/assets/javascripts/jquery.remotipart.js
and 和
app/assets/javascripts/jquery.iframe-transport.js
and instead added 而是添加了
//= require jquery.remotipart
to my application.js
file, and then it worked as expected. 到我的
application.js
文件,然后按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.