关闭

JavaScript提交form表单

82人阅读 评论(0) 收藏 举报
分类:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'upload.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript">
	$(function() {
		$('#button').click(function() {
			var oData = new FormData(document.forms.namedItem("form1" ));  
			/* oData.append( "CustomField", "This is some extra data" );  
			var oReq = new XMLHttpRequest();  
			oReq.open( "POST", "uploadServlet" , true );  
			oReq.onload = function(oEvent) {  
			      if (oReq.status == 200) {  
			    	 alert(oReq.responseText) ;
			     } else {  
			          alert('error!') ;  
			     }  
			};  
			oReq.send(oData);   */
			/* var formData = new FormData($("#form1")[0]); */
			 $.ajax({
				type : 'post',
				data : oData,
				async : false,
				cache : false,
				processData : false,
				contentType : false,
				url :'uploadServlet',
				dataType : 'text',
				success : function(data) {
					alert(data);
				},
				error : function() {
					alert('提交失败');
				}
			});
		});
	});
</script>
</head>
<body>
	<form name="form1" >
		文件名:<input type="text" name="filename" /> 选择文件:<input type="file"
			name="filepath" /> 用户名:<input type="text" name="username" /> 用户名:<input
			type="text" name="username" /> <input type="button" id="button"
			value="上传" />
	</form>
</body>
</html>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:512次
    • 积分:42
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档