IE8下struts2 采用ajax提交表单,返回json字符串,解析不了,直接保存json字符串。提示下载action.
但是在火狐 chrome下能够解析。
因为表单FORM是multipart-form方式提交的(enctype="multipart/form-data")。中间含有( <input type="file" id="logoImage" tabindex="4" name="logoImage" />)文件域。
提交后,提示下载action.并回调函数不能够正常拿到json字符串并不能解析。
解决办法:IE8下不能够正常获取返回的流的文件头,需要指定。
在struts2下的配置文件中添加:
<param name="contentType">text/html</param>
类似:
<action name="addMeeting" class="com.action.meeting.MeetingAction"
method="add">
<result type="json">
<param name="contentType">text/html</param>
<param name="root">resultMap</param>
</result>
</action>
参考代码如下:
<form id="addMeetingForm" action="${ctx}/pages/admin/pri/meeting/addMeeting.action" enctype="multipart/form-data">
<input type="hidden" id="meeting.id" name="meeting.id" value="${meeting.id}" />
<fieldset>
<legend>基本信息</legend>
<dl>
<dt>
<label for="title"><font color="red">* </font>名 称:</label>
</dt>
<dd>
<input style="width:80%" type="text" class="half" id="meeting_name" name="meeting.name" value="${fn:escapeXml(meeting.name)}" maxlength="40"/>
<small>请限制在40字以内,您还可以输入<span id="meeting_name_remain">40</span>字</small>
<!-- <small>如标题太长,则可输入空格进行换行设置,若标题中存在多个空格,则以第一个空格作为换行标志<span id="meeting_name_remain">40</span>字</small> -->
</dd>
</dl>
<dl>
<dt>
<label for="title"><font color="red">* </font>类 型:</label>
</dt>
<dd>
<c:if test="${meeting.id == null}">
<select id="meeting_type" name="meeting.meetingType.id">
<s:iterator value="meetingTypeList" var="mType">
<option value="${mType.id }" ${meeting.meetingType.id == mType.id ? "selected" : "" }>${mType.name }</option>
</s:iterator>
</select>
</c:if>
<c:if test="${meeting.id !='' && meeting.id != null}">
<label><wd:custom type="translate" entity="meeting_type" entityValue="id" entityName="name" selectedValue="${meeting.meetingType.id}" condition="where id=${meeting.meetingType.id}"></wd:custom></label>
<input type="hidden" id="meeting_type" name="meeting.meetingType.id" value="${meeting.meetingType.id}"/>
</c:if>
</dd>
</dl>
<dl>
<dt>
<label for="title"><font color="red">* </font>主 题:</label>
</dt>
<dd>
<textarea style="width:80%" class="medium" id="meeting.topic" name="meeting.topic" maxlength="2000">${fn:escapeXml(meeting.topic)}</textarea>
<small>请限制在2000字以内,您还可以输入<span id="meeting_topic_remain">2000</span>字</small>
</dd>
</dl>
<dl>
<dt>
<label for="title"><font color="red">*</font>主题皮肤:</label>
</dt>
<dd>
<div class="container">
<s:iterator value="pageThemeList" var="theme">
<div class="theme">
<div class="pic"><img src="${ctx}/css/${theme.thumbnailName}" /></div>
<div class="description">
<div class="title">
<label>
<input type="radio" name="meeting.pageTheme.id" value="${theme.id }" ${(meeting.pageTheme == null && theme.id==1) || meeting.pageTheme.id==theme.id ? "checked" : "" } /> ${theme.title}
</label>
</div>
<div class="comments">${theme.description}</div>
</div>
</div>
</s:iterator>
<div class="clear"></div>
<font id="tip_themeId" style="line-height:35px" color="red"></font>
</dd>
</dl>
<dl>
<dt>
<label for="title"><font color="red"></font>logo图片:</label>
</dt>
<dd>
<input type="file" id="logoImage" tabindex="4" name="logoImage" />
<br/>
<font id="tip_logoImage" style="line-height:35px" color="red"></font>
<small>请上传用于页面头部的logo图片,必须是背景透明的gif或png格式图片,最佳尺寸为108x53像素</small>
<s:if test="meeting.logoImage != null && meeting.logoImage.length() > 0">
<div id="logoDiv" class="logo">
<img src="${serverUrl}${meeting.logoImage }" width="108" height="53" border="0"/>
<small>已经设定的logo图片,点此<a href="javascript:deleteLogo(${meeting.id})">删除</a></small>
</div>
</s:if>
</dd>
</dl>
<dl>
<dt>
<label for="title"> 备 注:</label>
</dt>
<dd>
<textarea style="width:80%" class="medium" id="meeting.comments" name="meeting.comments" maxlength="200">${fn:escapeXml(meeting.comments)}</textarea>
<small>请限制在200字以内,您还可以输入<span id="meeting_comments_remain">200</span>字</small>
</dd>
</dl>
</fieldset>
<div class="page_form_sub">
<a href="##" οnclick="save();" id="submitBtn" class="btn_common btn_true">保 存</a>
</div>
</form>
参考JS:
function save(){
if($("#submitBtn").hasClass("btn_false")){
alert("您已经添加过本次编辑内容,请不要重复保存!");
return false;
}
//判断会议加会时间是否在开始和结束时间之内
$("#err_joinTime").html("");
var joinTime =$("#joinTime").val();
if(joinTime != "") {
var startTime = $("#startTime").val();
var endTime = $("#endTime").val();
if(joinTime < startTime || joinTime > endTime) {
return false;
}
}
submit();
//document.getElementById("submitBtn").disabled = "disabled";
}
function submit(){
var url = '';
if('${meeting.id}'==''){
url= '${ctx}/pages/admin/pri/meeting/addMeeting.action';
}else{
url= '${ctx}/pages/admin/pri/meeting/modifyMeeting.action';
}
var options = {
beforeSubmit: validate,
url: url,
success: callback,
type: 'post',
dataType: 'json'
};
$('#addMeetingForm').ajaxSubmit(options);
} //回调函数
function callback(data){
hideLoading();
alert(data.retmsg);
if(data.retcode=="0"){
showEditMenu("${ctx}",data.retdata.id);
//gotobase();
location.href = "${ctx}/pages/admin/pri/meeting/getMeetingById.action?returnType=manage_baseinfo&meeting.id=" + data.retdata.id;
}else{
$("#submitBtn").removeClass("btn_false");
$("#submitBtn").addClass("btn_true");
}
}