spring MVC 返回一段js语句到隐藏iframe中,页面未执行该js语句中的js方法

liubingyu12345 2015-12-01 04:04:13
预期做一个文件上传功能,点击文件提交后,controller返回字符串“<script>uploadXXX()</script>”到隐藏iframe中,期望controller执行执行后,就能执行uploadXXX()这个js方法。
现在问题是controller能够正常返回这个“<script>uploadXXX()</script>”到iframe中,但是却没有执行uploadXX方法。请问是什么原因?
提交文件的JSP代码片段:

<script type="text/javascript">
function updateload() {
$("#upForm1").submit();
}

var uploadFailed = function() {
alert(888);
}
</script>

........
<th align="right">附件</th>
<td colspan=5>
<table style="border-width:0px">
<tr>
<td style="border-width:0px">
<form id="upForm1" action="${ctx}/upload/upAnnex.htm" enctype="multipart/for-data" method="post" target="hidden_frame">
<a class=btn_addPic href="javascript:void(0);"><SPAN><EM>+</EM>上传附件</SPAN>
<INPUT class="filePrew" tabIndex=3 type=file size=3 name="file" id="file" onchange="updateload()"> </a>
<iframe name="hidden_frame" id="hidden_frame" ></iframe>
</form>
</td>
后台controller方法:
@RequestMapping(value = "/upAnnex", method = RequestMethod.POST)
@ResponseBody
public Object upAnnex(HttpServletRequest request, HttpServletResponse response,
BindException errors, PlAnnex annex) {
//执行一系列上传操作
........
return "<script>window.parent.uploadFailed();</script>";
}


...全文
296 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ranjio_z 2015-12-02
  • 打赏
  • 举报
回复
在你的Controller的上传方法upAnnex中,设置一个属性:request.setAttribute("success",false); 然后这个upAnnex方法返回页面为你的那个jsp页面,在你的那个jsp页面中用el表达式与js结合的方式,显示上传失败的提示框 var success="${success}"; if(success=="true"){ alert("上传成功"); }else if(success=="false"){ alert("上传失败,请重新上传"); } 注意,这段代码不要写在方法里面,而是直接写在<script>标签内
liubingyu12345 2015-12-02
  • 打赏
  • 举报
回复
没用//
引用 5 楼 scmod 的回复:
@responsebody那个方法返回类型改成String有用么
scmod 2015-12-01
  • 打赏
  • 举报
回复
@responsebody那个方法返回类型改成String有用么
liubingyu12345 2015-12-01
  • 打赏
  • 举报
回复
我换了个思路,不用spring的@ResponseBody 注解,直接通过参数里的response写入script语句,就OK了。如下形式: @RequestMapping(value = "/upAnnex", method = RequestMethod.POST,produces = "text/html;charset=UTF-8") public void upAnnex(HttpServletRequest request, HttpServletResponse response, BindException errors, PlAnnex annex) throws IOException { response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("<script>window.parent.uploadFailed();</script>"); response.getWriter().close(); } //为什么 用@responsebody注解不行呢,我是参照网上的例子,例子上显示是OK的啊。是不是我哪里理解有误?
liubingyu12345 2015-12-01
  • 打赏
  • 举报
回复
求大神们帮忙看下。。
_南天北落 2015-12-01
  • 打赏
  • 举报
回复
用jspf做,试试。可以网上查下JSPF。
liubingyu12345 2015-12-01
  • 打赏
  • 举报
回复
通过浏览器调试工具看了后台html,返回的script语句被<pre>标签包裹住了,而且是在<body>标签中,怎么才能使得返回的字符串写入到<head>标签中,且不被<pre>标签包裹住呢??

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧