当前位置: 代码迷 >> Ajax >> springmvc jquery ajax 提交复杂对象,415异常解决办法
  详细解决方案

springmvc jquery ajax 提交复杂对象,415异常解决办法

热度:3401   发布时间:2014-03-01 00:37:29.0
springmvc jquery ajax 提交复杂对象,415错误解决方法
var formdata = {};
formdata.backup = $("input[name='backup']:checked").val();
formdata.autobackManner = $("select[name='autobackManner'] option:selected").val();
formdata.partitionCount = $("select[name='partitionCount'] option:selected").val();
formdata.enable = $("input[name='enable']:checked").val();
if (formdata.backup == 'local') {
	formdata.local = {};
	formdata.local.path = $('#local_path').val();
} else {
	formdata.ftp = {};
	formdata.ftp.serverip = $("input[name='serverip']").val();
	formdata.ftp.user = $("input[name='user']").val();
	formdata.ftp.password = $("input[name='password']").val();
	formdata.ftp.encoding = $("select[name='encoding'] option:selected").val();
}
$.ajax({
	url: '/sim/systemConfig/modifyLogBackupConfig?configId=log_backup_config',
	type: 'POST',
	data: JSON.stringify(formdata),//*****转换成JSON对象
	dataType:'json',
	contentType:"text/javascript",//*****和application-servlet.xml配置相同
	success: function(res){
	}
});

?

<mvc:annotation-driven>
	<mvc:message-converters register-defaults="false">
		<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
		<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
		<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
		<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
			<property name="supportedMediaTypes">
				<list>
					<!-- *****自定义JSON返回类型***** -->
					<value>text/javascript;charset=UTF-8</value>
				</list>
			</property>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>

?

@RequestMapping("modifyLogBackupConfig")
@ResponseBody
public Result modifyLogBackupConfig(String configId,/*加入注解*/@RequestBody LogBackupConfig config){
	Result result = new Result(true, "保存成功!");

	return result;
}

?

public class LogBackupConfig implements Serializable {
	private String backup;
	private String autobackManner;
	private String partitionCount;
	private boolean enable;
	private FTP ftp;
	private Local local;
	
	//*****提供无参构造函数*****//
	public LogBackupConfig() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getBackup() {
		return backup;
	}

	public void setBackup(String backup) {
		this.backup = backup;
	}

	public String getAutobackManner() {
		return autobackManner;
	}

	public void setAutobackManner(String autobackManner) {
		this.autobackManner = autobackManner;
	}

	public String getPartitionCount() {
		return partitionCount;
	}

	public void setPartitionCount(String partitionCount) {
		this.partitionCount = partitionCount;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public FTP getFtp() {
		return ftp;
	}

	public void setFtp(FTP ftp) {
		this.ftp = ftp;
	}

	public Local getLocal() {
		return local;
	}

	public void setLocal(Local local) {
		this.local = local;
	}

	class FTP implements Serializable {
		private String serverip;
		private String user;
		private String password;
		private String encoding;
		//*****提供无参构造函数*****//
		public FTP() {
			super();
			// TODO Auto-generated constructor stub
		}

		public String getServerip() {
			return serverip;
		}

		public void setServerip(String serverip) {
			this.serverip = serverip;
		}

		public String getUser() {
			return user;
		}

		public void setUser(String user) {
			this.user = user;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getEncoding() {
			return encoding;
		}

		public void setEncoding(String encoding) {
			this.encoding = encoding;
		}

	}

	class Local implements Serializable {
		private String path;
		//*****提供无参构造函数*****//
		public Local() {
			super();
			// TODO Auto-generated constructor stub
		}

		public String getPath() {
			return path;
		}

		public void setPath(String path) {
			this.path = path;
		}

	}

}

?

以上代码注释部分是特别需要注意的。

  相关解决方案