This type of submission must match Multipartresolver,
$ ("Button:submit"). Click (function() {$.ajax ({type:' POST ', URL:' ${sys_config.root_path}/login.html ', Cache:false, ProcessData:false, ContentType:false, Data:NewFormData ($ (' #login_form ') [0]), DataType:"JSON"}). Done (function(data) {if(data.success) {//... } }); return false; });
Need to be used in the Spring config file, without this, the Parametersmap in request is empty
<BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--One of the properties available; The maximum file size in bytes - < Propertyname= "Maxuploadsize"value= "10240000"/> </Bean>
You want to add commons-fileupload in dependency.
In the controller, you can use the normal way
@RequestMapping (value = {"/login"}, method = Requestmethod.post, produces= "Application/json;charset=utf-8") @ResponseBody public String dologinpost () { Map<string, string[]> params = getrequest (). Getparametermap (); }
In addition, in the spring configuration also need to pay attention to add content-negotiation-manager, without this, using produces= "Application/json" will be 406 errors. One of the key parameters is favorpathextension, this must be false, and the other ignoreacceptheader this parameter can not be added, plus it will cause 406.
<Mvc:annotation-drivenContent-negotiation-manager= "Contentnegotiationmanager" /> <BeanID= "Contentnegotiationmanager"class= "Org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <!--Turn off working out content type based in URL file extension, should fall back to looking at the Accept headers - <!--Disabled path extension. Note that favor does isn't mean use one approach in preference to another, it just enables or disables it. The order of checking is always path extension, parameter, Accept header - < Propertyname= "Favorpathextension"value= "false" /> <!--Enable the use of the URL parameter - < Propertyname= "Favorparameter"value= "true" /> <!--Don ' t use the JAF, instead specify the media type mappings manually-we only wish-support JSON and XML - < Propertyname= "Usejaf"value= "false"/> < Propertyname= "Defaultcontenttype"value= "text/html" /> < Propertyname= "MediaTypes" > <value>Json=application/json Xml=application/xml</value> </ Property> </Bean>
For normal commits, this is the way to do it, without the need for multipartresolver
$ ("Button:submit"). Click (function() { $.ajax ({ ' POST ', ' ${sys_ Config.root_path}/login.html ', cache:false, data: $ (' #login_form '). Serialize (),//new FormData ($ (' #login_form ') [0]), dataType: "JSON" }). Done ( function(data) { //... }); return false ; });
Favorpathextension
Ajax submissions for SPRINGMVC