SpringMVC的Ajax提交

这种类型的提交, 必须配合 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

posted on   Milton  阅读(284)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示