1. 程式人生 > >Ajax SpringMVC 提交表單資料

Ajax SpringMVC 提交表單資料

Ajax 的寫法

function saveSplash() {
		var titleVal = $("#title").val();
		var subTitleVal = $("#subTitle").val();
		if (titleVal == null || "" == titleVal) {
			alert("標題不能為空");
			return;
		}

		if (subTitleVal == null || "" == subTitleVal) {
			alert("子標題不能為空");
			return;
		}
		var img = imgUrl.substring(imgUrl.lastIndexOf("/")+1, imgUrl.length);
		var data={"title":titleVal,"subTitle":subTitleVal,"type":4,"imageUrl":img};
		$.ajax({
			type : 'POST',
			url : '${pageContext.request.contextPath}/platform/article/ads',
			contentType: "application/json; charset=utf-8", 
			data : JSON.stringify(data),
			dataType : 'json',
			error : function(data) {
				alert("請求失敗,網路異常")
				console.log(data);
			},
			success : function(data) {
				var code = data.status;
				if (code == 200) {
					window.location.href = "index";
				} else {
					alert("提交失敗");
				}
			}
		});
	}

SpringMVC的寫法:

@RequestMapping(value="/ads", method = RequestMethod.POST)
@ResponseBody
public Object saveArticleAds(@RequestBody ArticleAds articleAds, HttpServletRequest request) {
xxxxx
xxxxx
return StatusConfig.OK;
}
}