spring mvc ajax 400解决

The request sent by the client was syntactically incorrect.

ajax发起请求时报400错误。请求代码如下:

复制代码
var reportId=($(obj).parent().parent().parent().children(":first").attr("value"));
            var isChecked=$(obj).prop("checked")=="checked"?1:0;
            var reportSetting=$(obj).attr("value");
            var setting={reportId:reportId,isChecked:isChecked,reportSetting:reportSetting};
            console.log(JSON.stringify(setting));
            $.ajax({
                type: "POST",
                url: "/reportConfiguration",
                contentType:"application/json",
                data:JSON.stringify(setting),
                dataType: "json",
                success: function (msg) {
                    if (msg.isSuccess){
                        $("#msg").html("设置成功")
                    }else{
                        $("#msg").html(msg.result);
                    }
                }
            });
复制代码

服务端代码:

@RequestMapping("/reportConfiguration")
    @ResponseBody
    public String reportSet(@RequestBody ReportSettingEditBean reportSettingEditBean,HttpServletRequest request){
        return "";
    }

bean定义:

复制代码
public class ReportSettingEditBean {
    private long reportId;

    private boolean isChecked;

    private ReportSetting reportSetting;

    public long getReportId() {
        return reportId;
    }

    public void setReportId(long reportId) {
        this.reportId = reportId;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
    }

    public ReportSetting getReportSetting() {
        return reportSetting;
    }

    public void setReportSetting(ReportSetting reportSetting) {
        this.reportSetting = reportSetting;
    }
}

public enum ReportSetting {
    Fixed(1),
    Scroll(2),
    First(4);

    private int value;

    public int getValue() {
        return value;
    }

    ReportSetting(int value){
        this.value=value;
    }
}
复制代码

解决:

在js中核对数据类型与接收数据类中属性的数据类型是否一致。

上例中:ReportSetting 是枚举对象, 而var reportSetting=$(obj).attr("value") 是字符串。修改成整数即可。正确请求如下:

复制代码
var reportId=($(obj).parent().parent().parent().children(":first").attr("value"));
            var isChecked=$(obj).prop("checked")=="checked"?1:0;
            var reportSetting=parseInt($(obj).attr("value"));
            var setting={reportId:reportId,isChecked:isChecked,reportSetting:reportSetting};
            $.ajax({
                type: "POST",
                url: "/reportConfiguration",
                contentType:"application/json",
                data:JSON.stringify(setting),
                dataType: "json",
                success: function (msg) {
                    if (msg.isSuccess){
                        $("#msg").html("设置成功")
                    }else{
                        $("#msg").html(msg.result);
                    }
                }
            });
复制代码

 

posted @   tyb1222  阅读(3033)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示