SpringMVC的Ajax提交

來源:互聯網
上載者:User

標籤:

這種類型的提交, 必須配合 multipartResolver,

$("button:submit").click(function(){      $.ajax({        type : ‘POST‘,        url : ‘${sys_config.root_path}/login.html‘,        cache:false,        processData:false,        contentType:false,        data : new FormData($(‘#login_form‘)[0]),        dataType : "json"      }).done(function(data) {        if (data.success) {          //...        }      });      return false;    });

需要在Spring設定檔中使用, 沒有這個的話, request中的parametersMap是空的

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <!-- one of the properties available; the maximum file size in bytes -->        <property name="maxUploadSize" value="10240000"/>    </bean>

在dependency中要添加commons-fileupload.

在Controller中, 使用正常的方式即可

@RequestMapping(value = {"/login"}, method = RequestMethod.POST, produces="application/json;charset=UTF-8")    @ResponseBody    public String doLoginPost()    {        Map<String, String[]> params = getRequest().getParameterMap();        ..    }

另外在Spring的配置中還需要注意添加 content-negotiation-manager, 沒有這個的話, 使用produces="application/json"會出406錯誤. 其中關鍵的一個參數是favorPathExtension, 這個必須為false, 另外ignoreAcceptHeader這個參數不能加, 加上也會導致406.

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">        <!-- Turn off working out content type based on URL file extension, should fall back to looking at the Accept headers -->        <!-- Disabled path extension. Note that favor does not 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 -->        <property name="favorPathExtension" value="false" />        <!-- Enable the use of the URL parameter -->        <property name="favorParameter" value="true" />        <!-- Don‘t use the JAF, instead specify the media type mappings manually - we only wish to support JSON and XML -->        <property name="useJaf" value="false"/>        <property name="defaultContentType" value="text/html" />        <property name="mediaTypes" >            <value>                json=application/json                xml=application/xml            </value>        </property>    </bean>

對於普通的提交, 使用這種方式即可, 不需要multipartResolver

    $("button:submit").click(function(){      $.ajax({        type : ‘POST‘,        url : ‘${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

SpringMVC的Ajax提交

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.