简体   繁体   English

在Spring RestController的Ajax POST中出现400错误的请求错误

[英]400 bad request error in Ajax POST to spring RestController

I have ajax code as 我有一个ajax代码

/**
 * Ajax Logic for submitions
 * */
$.ajax({
    contentType : 'application/json; charset=utf-8',
    type: 'POST',
    url: '/domain/insert/',
    dataType : 'json',
    data : 'firstName:' + $("#first_name").val() /*+ "&lastName;=" + $("#lastName").val() + "&email;=" + $("#email").val()*/,
    success : function(callback){       
         console.log("Data inserted.........");
    },
    error : function(){
        console.log("Error.........");
    }
});

getting console out put as 将控制台作为

POST http://localhost:8080/domain/insert/ 400 (Bad Request)
Z.cors.e.crossDomain.send @ common.min.js:3
J.extend.ajax @ common.min.js:3
t.length.t.steps.onStepChanged @ forms_wizard.min.js:34
J.event.dispatch @ common.min.js:2
m.handle @ common.min.js:2
J.event.trigger @ common.min.js:2
J.fn.extend.triggerHandler @ common.min.js:2
(anonymous function) @ wizard_steps.min.js:1
c @ common.min.js:2
d.fireWith @ common.min.js:2
(anonymous function) @ common.min.js:2
c @ common.min.js:2
d.fireWith @ common.min.js:2
a @ common.min.js:2
c @ common.min.js:2
d.fireWith @ common.min.js:2
d.fire @ common.min.js:2
J.extend.dequeue @ common.min.js:2
i.complete @ common.min.js:2
c @ common.min.js:2
d.fireWith @ common.min.js:2
I.l @ common.min.js:1
J.fx.tick @ common.min.js:3
forms_wizard.min.js:47 
Error.........

My insert method in RestController is 我在RestController中的insert方法是

@RequestMapping(value = "/question/", headers="Accept=*/*", consumes="application/json", method = RequestMethod.POST)
    public ResponseEntity<Void> insert(@RequestBody User user) {
        System.out.println("Creating " + user.getFirstName()); 
        service.save(user);        
        return new ResponseEntity<Void>(HttpStatus.CREATED);
    }

OK, if I'm using Postman to POST data to controller it is succeeded but through above ajax code I'm getting 400 Error..... 好的,如果我使用Postman将数据发布到控制器,则成功,但是通过上面的ajax代码,我得到400错误.....

your data property is the problem 您的data属性是问题

you need to pass an json object in the data in order for this to work 您需要在数据中传递一个json对象,以使其工作

data : {username: 'username'}

Wanted to write this as a comment, but can't yet... 想要将其写为评论,但还不能...
What is your version of JQuery and if you post with Postman, what are your settings? 您的JQuery版本是什么?如果使用Postman进行发布,您的设置是什么?
Would you also give the network request for the request and response? 您还会为请求和响应提供网络请求吗?
Try to wrap data in brackets: {'firstName:' ... } 尝试将数据包装在方括号中: {'firstName:' ... }

I've had a similar problem in the past (without spring though) and had to set the consumes to 'application/json; charset=utf-8' 过去(虽然没有春季),我过去也遇到过类似的问题,必须将消费设置为'application/json; charset=utf-8' 'application/json; charset=utf-8' in the controller (although Intellij marks this as incorrect) 控制器中的'application/json; charset=utf-8' (尽管Intellij将其标记为不正确)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM