The data was inserted successfully. Why did ajax return the error function?

Code:
/ / send an asynchronous request for registration

    $.ajax({
        type:"post",
        url :"/user/register",
        data:$("form").serialize(),
        dataType:"json",
        success:function(data){
            alert(data);
            if(data == "success"){
                alert("");
                window.location.href="/";//
            }else if(data == "fail"){
                alert(":"+data);
            }

        },
        error:function(data){
            alert("error:"+data);
            if(data == "error")
            alert(""+data);
            window.location.href="/";
        }
    });

@ RequestMapping (value = "/ user/register", method = RequestMethod.POST)

public ModelMap register(User user){
    ModelMap map = new ModelMap();
    try {
        System.err.print(user.toString());
        int result = userService.register(user);
        if (result > 0) {
            //
            map.put("res","success");
        }else {
            map.put("res","fail");
        }
        return map;
    }catch (Exception e){
        e.printStackTrace();
        map.put("data","error");
        return map;
    }
}

and window.location.href= "/"; this line of code is not executed because it does not jump

clipboard.png
the database inserts data and does not throw an exception, so why error?

Mar.11,2021

check whether response,http code is 200. If not, you will enter error logic


ajax set dataType: "json"
return format is not json will enter error


control layer return is not json format, you are missing @ ResponseBody


browser F12, look at the request in NetWork and you will find Status Code: 404 Not Found.

404@ResponseBody404.

then you annotate the @ ResponseBody method, and after running the project successfully, you will report HTTP Status 500-Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class org.springframework.ui.ModelMap again.
Why? Json type conversion failed.

fastjsonjackson

https://blog.csdn.net/qq_3524.

Menu