When using jquery's Ajaxform plugin for AJAX submission forms and uploading files, the return type Datatype:json but behind the scenes by writing out a JSON object, after execution did not enter the success function, but directly pop up a download window, The JSON string is now loaded for processing. It was later found that the solution was not to write JSON strings in JSON, but instead to "text/html" the JSON string into the textarea tag.
For example:
Response.setcontenttype ("Text/html;charaset=utf-8");
Response.getwriter (). Write ("<textarea>" +jsonstring+ "</textarea>");
This makes it possible to use JSON objects normally in JS.
Html:
<body>
<form id= "Uploadform" action= "${url}/uploadfile" method= "POST" >
English test: <input name= "FilePath" type= "file" ></input>
<button id= "Upload" > File detection </button>
</form>
</body>
$ (function () {
$ (' #upLoadForm '). Ajaxform ({
Success:function (data, StatusText, JQXHR, $form) {
alert (data);
}
});
});
Checkcontroller:
@RequestMapping (value = "/uploadfile", Method=requestmethod.post)
Public @ResponseBody void Processuploadd (httpservletresponse response,
@RequestParam (value = "FilePath") multipartfile file,httpsession HttpSession) {
String message= "No Chinese!" ";
Response.setcontenttype ("Text/html;charaset=utf-8");
try {
Response.getwriter (). write (message);
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
/*string filename=file.getoriginalfilename ();//Get Upload file name
if (filename = = NULL | | "". Equals (FileName)
{
return null;
}
try{
FileManager FileSave = new FileManager ();
Get Upload Path
String Filepath=filemanager.getuploadfilepath ();
Filesave.savefilefrominputstream (File.getinputstream (), filepath,filename);
String content = filemanager.readtxtfile (filePath + "\ \" + filename);
List<dataformat> errorlist = checken.checken (content);
String info = "";
for (int i=0; i<errorlist.size (); i++) {
String cn = Errorlist.get (i). GETCN ();
String pub = Errorlist.get (i). GetErrorMessage ();
info = pub + "Chinese is:" + cn;
}
if (info!= "") {
Message=info;
}
}
catch (Exception e)
{
E.printstacktrace ();
return message;
} */
return message;
}
jquery ajaxform Uploading a file returns a question without prompting information