SSM: json data request using AJAX and transmitting post RequestBody receive parameters, error analysis and resolution of 415 and 405

POST http://localhost:8080/blog/submitForm 415 (Unsupported Media Type)

POST http://localhost:8080/blog/submitForm 400 (Bad Request)

Demand: json string to a background, using ajax, when the results achieved, first the front end 415, 400 is given later. The whole process is wrong and the solution recorded for your reference.

Below is the code I ajax code and a method for receiving layer of the beginning of the back-end controller

    $(".layui-btn").click(function () {
        var params = {"title":$("#title").val(),"selectCategory":$("#selectCategory").val(),"content":$("#editor_container").val()}]
        $.ajax({
            type: "post",
            url: "/blog/submitForm",
            data: params,
            dataType: "json",
            success: function(result){
            }
        });
    })
   @RequestMapping(value = "/submitForm", method = RequestMethod.POST)
    @ResponseBody
    public String submitForm(@RequestBody JSONObject data){
        System.out.println("进入到submitForm");
        System.out.println(data.get("title"));
        System.out.println(data.get("content"));
        return "";
    }

Run-time error 415:

POST http://localhost:8080/blog/submitForm 415 (Unsupported Media Type)

Cause: Missing contentType: 'application / json'

This is because the background is json object receives parameters, so you want to set up here at the request ajax.

This line is used to tell the server to process the request body should be what kind of way. Equivalent, you will request data body after sent out, you must also tell the server, the data I send what type of, so that the server can parse the contents of the request body correctly (if no content-type or content-type settings is wrong, will cause the browser to request body could not be resolved correctly, it will not be able to make the right response to the data.

After the above plus warm talk about the code, run the 400 error prompt:

POST http://localhost:8080/blog/submitForm 400 (Bad Request)

The reason: lack of data: JSON.stringify (params),

I'm not json code transmission type, and receive my background, it is to receive json object, need to be json of params.

The final correct code is as follows

 var params = {"title":$("#title").val(),"selectCategory":$("#selectCategory").val(),"content":$("#editor_container").val()}
        $.ajax({
            type: "post",
            url: "/blog/submitForm",
            data: JSON.stringify(params),
            dataType: "json",
            contentType:'application/json',
            success: function(result){
            }

Summary: Whether 415 or 400 error, are "request sent by the client is not syntactically correct." That

So using ajax requests more attention to your request header settings.

Quote: https://www.jianshu.com/p/3716212666d6

https://www.cnblogs.com/yeyuchangfeng/p/5623445.html

Published 22 original articles · won praise 3 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41623154/article/details/101237885