ajax 提交form表单数据

一、ajax传输非json;

$("form").serialize(),序列话表单结果:序列得到字符串类型为:a=1&b=2&c=3;

此时不用设置contentType(定义的是发送至服务器的数据类型,data-Type定义的是服务器返回的数据),后端接收参数不需要@Requestbody
ajax 默认的contentType是:application/x-www-form-urlencoded
$.ajax({
    type: 'post',
    url: 'your url',
    data: $("form").serialize(),
    success: function(data) {
        // your code
    }
});

后端代码:

@RequestMapping("/testStr")
@ResponseBody
public R testStr(TestEntity entity){ return R.of(service.testCreate(entity)); }

二、ajax传输json

serializeJSON(),自定义的函数;把序列号的form数据 转成json格式

var json =  $("form").serializeJSON();
$.ajax({
    type: 'post',
    url: 'your url',
    data: JSON.stringify(json),//必须是字符串
    contentType:"applicantion/json",
    success: function(data) {
        // your code
    }
});

后端代码

@RequestMapping("/testStr")
@ResponseBody
public R testStr(@RequestBody TestEntity entity){
return R.of(service.testCreate(entity));
}

三、关于@RequestBody和@Responsbody的使用

https://www.cnblogs.com/lixiuming521125/p/7885741.html#_label1

 

posted @ 2021-09-15 14:39  啄木鸟伍迪  阅读(660)  评论(0)    收藏  举报
//火箭 GenerateContentList();