zoukankan      html  css  js  c++  java
  • ajax-springMVC提交表单的方式

    1.request参数提交(Form提交),适用于GET/POST

    request参数传递都会转换成 id=123&fileName=test.name&type=culture_art这种形式,get请求会显示在url上,post不在url上显示

    ajax写法:

    $.ajax({
            url : /admin/test,
            type : "post",
            async : true,//默认为true
            contentType: application/x-www-form-urlencoded, //默认值为表单提交
            data : {"id" : id,"fileName" : fileName,"type" : modelType},//请求参数
            success : function(data) {
                //请求成功处理
            },
            error : function(data) {
                //请求失败处理
            }
    });

    controller

    第1,2种写法controller形参和ajax data中的key必须保持一致

    第3种写法形参可以不一致,@requestParam中的vaue必须和data中的key一致才能自动参数绑定)

    第1种写法
    @PostMapping("/admin/test") public ResultBean<String> updateModelNameAndType(
      String id,
      String fileName,
      String type) {
    return null; }
    第2种写法 @PostMapping(
    "/admin/test") public ResultBean<String> updateModelNameAndType(
       @RequestParam String id,
      @RequestParam String fileName,
       @RequestParam String type) {
    return null; }
    第3种写法 @PostMapping(
    "/admin/test") public ResultBean<String> updateModelNameAndType(
      @RequestParam(value="id") String tmpId,
      @RequestParam(value="fileName") String tmpFileName,
      @RequestParam(value="type") String tmpType) { return null; }
    第4种写法(封装成bean,bean的属性名称和ajax data中的key一致即可自动绑定到对象)
    @PostMapping("/admin/test")
    public ResultBean<String> updateModelNameAndType(ModelInfo modelInfo) {
          return null;
    }

    2.request body提交,适用于POST提交

    注意将ajax中data的值json对象转成json字符串

    ajax

    $.ajax({
        url : "/admin/test",
        type : "post",
        contentType : "application/json;charset=UTF-8",
        data : JSON.stringify({id:"id",fileName:"test.txt"}),//或者直接获取form表单 data : $('#form').serialize()
        success : function(data) {
            
        },
        error : function(data) {
            
        }
    });

    controller

    @PostMapping("/admin/test")
    public ResultBean<String> insertNewVersion(@RequestBody VersionEntity versionInfo) {
      return service.getVersionSvc().insertVersion(versionInfo);
    }
  • 相关阅读:
    laravel5.5
    yii2.0 Activeform表单部分组件使用方法
    putty连接远程局域网的MySql(不需要单独打开plink)
    关于 CentOS 自启动(服务、脚本)
    make -j 多核并行编译 导致笔记本过热 自动关机保护
    CentOS 7 引导 -- GRUB2
    Centos 7 拨号上网(PPPOE)
    Centos 7 意外断电如何处理
    Windows/Linux 生成iOS证书及p12文件
    git add -f
  • 原文地址:https://www.cnblogs.com/zincredible/p/10236268.html
Copyright © 2011-2022 走看看