jQuery的形式插件/轨道HTML的文件上传/纯文本响应,而不是 [英] jquery form plugin / rails file upload / plain text respond instead of html

查看:107
本文介绍了jQuery的形式插件/轨道HTML的文件上传/纯文本响应,而不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个文件上传通过AJAX的形式来工作..因为我使用了jQuery插件的形式将文件上传效果很好..但在respond_to代码块的JS呈现的HTML返回为纯文本。希望有人能帮助

i'm trying to get a file upload via ajax form to work.. as i am using the jquery form plugin the file upload works well.. but the rendered html in the respond_to js block is returned as plain text. hope someone can help

控制器:

    如果@ visitreport.save
    闪光[:通知] =明镜Besuchsbericht wurde erstellt。
    respond_to代码做|格式|
      的format.html {redirect_to时@visitreport}
      format.js
    结束

if @visitreport.save
flash[:notice] = "Der Besuchsbericht wurde erstellt."
respond_to do |format|
format.html{redirect_to @visitreport}
format.js
end

create.js.erb:

create.js.erb:

    $(#last_customers)replaceWith('< D​​IV ID =last_customers><%= escape_javascript(渲染:部分=>的客户/ last_customers)%>< / DIV>');
    $(#提醒)replaceWith('< D​​IV ID =提醒><%= escape_javascript(渲染:部分=>中的客户/提醒:当地人=> {:日期=> Date.today + 1})%>');
..和一些其他的jQuery - 魔术

$("#last_customers").replaceWith('<div id="last_customers"><%= escape_javascript(render :partial => 'customers/last_customers') %> </div>');
$("#reminder").replaceWith('<div id="reminder"><%= escape_javascript(render :partial => "customers/reminder", :locals => {:date => Date.today+1}) %>');
.. and some other jquery - magic

_last_customers.html.haml: - 只是举例

_last_customers.html.haml: -- just for example

%H5模具zuletzt bearbeiteten Kunden
%小时
.subcolumns
  -get_user_customers.each做| UC |
    .c25l
      .subcl
        =的link_to(#{uc.id},customer_path(uc.id))
    .c75l
      .subcl
        =的link_to(#{get_customer_name(uc.id)},customer_path(uc.id))

%h5 Die zuletzt bearbeiteten Kunden
%hr
.subcolumns
-get_user_customers.each do |uc|
.c25l
.subcl
=link_to("#{uc.id}", customer_path(uc.id))
.c75l
.subcl
=link_to("#{get_customer_name(uc.id)}", customer_path(uc.id))

响应看起来是这样的:

&放大器; LT; D​​IV CLASS =报告round_corners box_shadow&放大器; GT;&放大器;其中,H5和放大器; GT; bearbeitet上午Freitag的,04.六月2010,12:06 UHR和放大器; LT; / H5和放大器; GT; &放大器; LT; D​​IV CLASS ='文本右'和; GT;每个电话时05.07.2010与放大器; LT; / DIV&放大器; GT; &放大器;其中,P级='report_content'和; GT; FG&安培;其中; / P&安培; GT; &放大器; LT; D​​IV CLASS ='文本右提醒'和; GT; Wiedervorlage是二○一五年十二月一十四日&放大器; LT; BR /&放大器; GT; &放大器; LT; A HREF =/ visitreports / 138 /编辑&放大器; GT; Bericht bearbeiten和放大器; GT;&放大器; GT;&放大器; LT; / A&放大器; GT; &放大器; LT; / DIV&放大器; GT; &放大器;其中,H5和放大器; GT; Dateien和放大器; LT; / H5和放大器; GT; &放大器; LT; A HREF =/资产/ 27&放大器; GT; bg_mainmenu.gif和放大器; LT; / A&放大器; GT; &放大器; LT; / DIV&放大器; GT;

application.js中:

application.js:

("#new_visitreport").submit(function() {
$(this).ajaxSubmit(
dataType  : 'script',
  iframe    : true,
success   : function (data) {
    data = eval(data.replace("<pre>", "").replace("</pre>", ""));
  }
});
return false;
});

我在做什么错了? 希望有人能帮助! jQuery的1.4.2版

what am i doing wrong? hope somebody can help! jquery version 1.4.2

推荐答案

据jQuery的形式文档,建议换行文本区域的响应。例如,在再培训局:

According to the jQuery Form documentation, it is recommended to wrap the response in textarea. Example in erb:

<textarea>
  jQuery("#documents").prepend("<%= escape_javascript(render @document) %>");
</textarea>

然后将内容类型设置为的text / html 控制器上:

respond_to do |format|
  # jQuery Form needs to have text/html as Content-Type.
  format.js { render :content_type => Mime::HTML.to_s }
end

这篇关于jQuery的形式插件/轨道HTML的文件上传/纯文本响应,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆