Java 提交表单而不重新刷新页面
我有以下代码:Java 提交表单而不重新刷新页面,java,jquery,ajax,jsp,Java,Jquery,Ajax,Jsp,我有以下代码: (function myFunction() { $('form#uploadform').on('submit', function(e) { $.post('UploadServlet', $(this).serialize(), function (data) { // This is executed when the call to mail.php was succesful. // 'data' contains the re
(function myFunction() {
$('form#uploadform').on('submit', function(e) {
$.post('UploadServlet', $(this).serialize(), function (data) {
// This is executed when the call to mail.php was succesful.
// 'data' contains the response from the request
}).error(function() {
// This is executed when the call to mail.php failed.
});
e.preventDefault();
});
return false;
});
</script>
<form action="UploadServlet" method="post" id="uploadform"
enctype="multipart/form-data">
<input type="file" name="picture" size="50" placeholder="Browse Image" />
<br />
<button onclick="myFunction()">Submit</button>
</form>
(函数myFunction(){
$('form#uploadform')。在('submit',函数(e)上{
$.post('UploadServlet',$(this).serialize(),函数(数据){
//这是在成功调用mail.php时执行的。
//“数据”包含来自请求的响应
}).错误(函数(){
//这是在调用mail.php失败时执行的。
});
e、 预防默认值();
});
返回false;
});
提交
我有一个上传文件功能,我想上传文件,但想保持在同一页上,我尝试了上述功能,但不起作用,我被重定向到另一页?您在
submit
上运行的功能需要返回false
,这样就不会通过浏览器正常提交表单。您确定在提交时调用了您的函数(执行$.post的函数)吗?我做了以下更改:添加了提交,但仍然没有更改,仍然重定向到不同的页面。这样做并没有调用servlet,这是我想要的