通过AJAX和Web服务提交表单的Java脚本
$('#btnSignup').click(function(){
var user = {"id": 10, "firstName": $('#firstName').val(), "lastName": $('#lastName').val(), "age": 69, "freeText": "55555", "weight": 55};
$.ajax({
type: "POST",
contentType: "application/json",
url: "http://localhost:8080/testSoft/webresources/entity.user/",
dataType: "json",
data: JSON.stringify(user),
//data: $("#register-form").serialize(), // serializes the form's elements.
success: function(response) {
alert('response: '+response);// This is always NULL?
},
error: function(data) {
alert('addUser error: ');
}
});
});
提交后未关闭的模式对话框
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Sign Up</h3>
</div>
<div class="modal-body">
<form id="register-form" onsubmit="return false;">
<div class="register-form-block">
<input type="text" value='Your first name' name="firstName" id="firstName" onclick="this.value = ('')"/>
</div>
<div class="register-form-block">
<input type="text" value='Your surname' name="lastName" id="lastName" onclick="this.value = ('')" />
</div>
<div class="register-form-block">
<input type="text" value='Username' name="username" id="username" onclick="this.value = ('')"/>
</div>
<div class="register-form-block">
<input type="password" value='Password' name="password" id="password" onclick="this.value = ('')"/>
</div>
<div class="register-form-block">
<input type="password" value='Password again' name="passwordagain" onclick="this.value = ('')"/>
</div>
<!--a href="javascript:doRegister();" class="register-button" id="btnAddUser" >Register</a-->
<br/>
<button type="button" class="btn btn-primary btn-large" id="btnSignup">Register</button>
</form>
</div>
</div>
问题
1)为什么响应始终为NULL?请求是通过Web服务成功向DB添加数据。
2)为什么提交表单后模态对话框没有关闭?我希望关闭对话框并更新一个div并告诉用户注册正常。
3)如何在无错误提交后告诉服务器转到其他页面,例如www.google.com或我的应用程序中的web服务器中的其他页面,如thankYou.html? 我在Jquery + Ajax中有一个FLAT学习曲线:(这让我很疯狂。
感谢您的帮助! 萨米