AJAX 表单序列化

###文件表单

html:

<form id="uploadForm" enctype="multipart/form-data" style="margin-left: 550px;margin-top: 150px;">

账户:<input type="text" name="username" id="username"><br>

密码:<input type="text" name="password" id="password"><br>

电话:<input type="text" name="phone" id="phone"> <br>

地址:<input type="text" name="address" id="address"><br>

图片:<input type="file" name="file" id="file"/><br>

姓名:<input type="text" name="wname" id="wname"><br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" value="1" checked name="hobby" />成为用户<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" value="2" checked name="hobby" />成为商户<br>

<input type="button" onclick="register()" value="提交" style="margin-left: 34px;width:149px;height:66px;">

</form>

js:

</script>

function register(){

var formData = new FormData($("#uploadForm")[0]);

$.ajax({

url: '<%=basePath%>insert.do' ,

type: 'POST',

data: formData,

async: false,

cache: false,

contentType: false,

processData: false,

success: function (returndata) {

alert("wwww");

},

error: function (returndata) {

alert("ssss");

}

});

}

</script>

###正常表单

<script type="text/javascript">

function login(){

var name=$("#user").val();

var password=$("#pwd").val();

$.ajax({

type: "POST",

url:'<%=basePath%>login.do',

data:$('#user').serialize(), // 表单序列化

success : function(result) {

if(result == "success_1")

window.location.href="Authorization_page/user.jsp";

if(result == "success_2")

window.location.href="Authorization_page/commercial_tenant.jsp";

if(result == "success_3")

window.location.href="Authorization_page/Administrators.jsp";

if(result == "error")

alert("用户名密码不正确!");

},

error : function(error){

}

});

}

</script>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区