Ajax submissions for SPRINGMVC

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.