Spring MVC + AJAX + Jquery ajax callback to success function is not working
来源:互联网 发布:数据传输线 编辑:程序博客网 时间:2024/06/06 10:03
There is some problem with callback function of Ajax by returning string value from Spring Controller. I want the image to be uploaded asynchronously, Upload is successful but the page is redirecting to the another page and the callback function is not called.
JSP: image-upload.jsp
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>some title</title><script src="<c:url value="/resources/scripts/jquery.js" />"></script><link rel="stylesheet" href="<c:url value="/resources/css/jquery-ui.css" />"><script src="<c:url value="/resources/scripts/jquery-ui.js" />"></script><script type="text/javascript"> $(document).ready(function(){ $("#popsave").click(function(e){ $.ajax({ url: $("#uploadform").attr( "action"), data: {fileToUpload : formData}, type: 'POST', cache: false, processdata: false, contentType : false, async: true, dataType: "json", mimeType: "application/json", success: function(data){ alert(JSON.stringify(data)); }, error: function(){ alert("something went wrong"); } }); e.preventDefault(); return false; }); }); </script></head><body> <!-- image uploading pop up --> <form method="post" action="/SampleUpload/uploadfile" id="uploadform" enctype="multipart/form-data"> <input type="file" name="fileToUpload" id="fileToUpload" /> <table style="align:center;"><tr><td><input id="popsave" type="submit" value="Save" /></td><td><input id="cancelBtn" type="button" value="cancel"/></td></tr></table> <img id="cancelpop" style="width:20px;height:20px;position: absolute;top:-15px;right:-15px;" src="<c:url value='/resources/images/cancel.png'/>" alt="cancel"/> </form></body></html>
My Controller:
@RequestMapping(value = "/uploadfile", method = RequestMethod.POST) public @ResponseBody String uploadImage(HttpServletRequest request, @RequestParam(value = "fileToUpload") MultipartFile image) throws IOException { String fileName = image.getOriginalFilename(); String imageurl = ""; try { if(fileName != ""){ String path = (String) request.getSession().getServletContext().getRealPath("/"); String rndfilename = RandomStringUtils.randomAlphanumeric(20); File file = new File(path+"resources/tempuploads/"+ rndfilename + fileName); imageurl = path+"resources/tempuploads/"+ rndfilename + fileName; fileName = file.toString(); FileUtils.writeByteArrayToFile(file, image.getBytes()); System.out.println("Go to the location: " + file.toString() + " on your computer and verify that the image has been stored."); } } catch (IOException e) { throw e; } return imageurl; }
The "imageurl" should be returned to my current jsp page(image-upload.jsp) and have to show an alert msg. But it is showing the "imageurl" on the other page (showing on the URL which we have called in ajax i.e http://www.test.com/SampleUpload/uploadfile). There is no page with name 'uploadfile'. I have tried different ways but didn't worked. Please help me to fix it ( I want to return the image url to our ajax callback function and have to show alert msg in the same page and the page shouldn't be reloaded or refreshed or redirected). I am also using Spring security in my application.
Answer:
Remove the action attribute from the form, and directly give URL in your ajax call.
- Spring MVC + AJAX + Jquery ajax callback to success function is not working
- 在Spring MVC使用ajax中的success
- AJAX问题:jQuery.handleError is not a function
- JQuery AJAX Post Not Working On IE8
- jquery ajax success beforeSend
- [Spring MVC] What is my Spring MVC controller not working?
- extjs ajax callback:function(options,success,response)中几个参数的含义
- spring mvc 与 jquery ajax
- spring mvc 与 jquery ajax
- Spring MVC Ajax and JQuery
- Spring MVC Ajax and JQuery
- spring mvc 与 jquery ajax
- spring mvc json ajax jquery
- jQuery.form Ajax无刷新上传报错 (jQuery.handleError is not a function) 解决办法
- jQuery.form Ajax无刷新上传报错 (jQuery.handleError is not a function) 解决办法
- jquery ajax图片上传 springmvc ajaxFileUpload 报这错jQuery.handleError is not a function
- jquery ajax 不执行success
- Jquery ajax 不执行success
- Valid Sudoku
- virtualbox使用记录
- Cmap的使用方法
- poj-2492- A Bug's Life
- Linux workqueue工作原理
- Spring MVC + AJAX + Jquery ajax callback to success function is not working
- TinyMCE多输入框手动激活解决方案(init()初始化后回调)
- C++文件流操作
- Apache Commons项目介绍
- JS用法(一)
- [2014-09-02]JAVA笔记_增强的for循环、自动拆箱装箱、可变参数
- 六 (6.7)C/C++运行库注意事项 6.8 了解自己的身份
- 我遇到的java笔试题->Swing组件JList的列表数据修改了,如何通知JList改变显示?
- BZOJ 3670 NOI 2014 动物园 变形KMP