Javascript 无法加载资源:服务器响应状态为 400(错误请求)Spring JS 对控制器的调用不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43170857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to load resource: the server responded with a status of 400 (Bad Request) Spring JS call to Controller not working
提问by JovanGacic
I'm trying to call a controller method that will save my object, but when i try to url to it, it returns http error. I've browsed through some similar problems on SO but no luck. So i wanna ask myself...
我正在尝试调用一个控制器方法来保存我的对象,但是当我尝试 url 到它时,它返回 http 错误。我在 SO 上浏览了一些类似的问题,但没有运气。所以我想问自己...
Here is my Ajax (for simplicity purposes i renamed the variables):
这是我的 Ajax(为了简单起见,我重命名了变量):
function addDob() {
var var1 = $("#var1").val();
var var2 = $("#var1").val();
var var3 = {};
var3["var3"] = $("#var3").val();
var json = JSON.stringify({
var1 : var1,
var2 : var2,
var3 : var3
});
console.log(json);
alert(json);
$.ajax({
url: 'add_dob',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: json,
success: function (data) {
console.log(json);
alert(data.message);
resetForm();
},
error: function () {
alert("Error!");
}
});
}
Here is my controller:
这是我的控制器:
@RequestMapping(value = "/add_dob", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public Map<String, Object> saveDob(@RequestBody DobavljacWrapper wrapper) {
Map<String, Object> data = new HashMap<>();
Dob d = new Dob();
d.setCountryID(wrapper.getCountryID());
d.setDobName(wrapper.getDobName());
d.setYear(wrapper.getYear());
dobService.save(d);
data.put("message", "Dob was successfully saved!");
return data;
}
Any suggestion is welcomed. If i need to insert any more info, just let me know. Cheers! P.S. I had a similar project that works, but my model classes were different, so i suspect there's something to it..
欢迎任何建议。如果我需要插入更多信息,请告诉我。干杯! PS我有一个类似的项目,但我的模型类不同,所以我怀疑它有什么问题..
UPDATE 1.0:
更新 1.0:
I figured out it has a lot to do with the @RequestBody parameter.
我发现它与@RequestBody 参数有很大关系。
That parameter matches the one you are pushing through with your Ajax. Now, I need that parameter to match my object which has the exact attributes I am passing through with Ajax. Somewhere in there i am making a mistake, and i'm not sure what exactly is the right way here...
该参数与您使用 Ajax 推动的参数相匹配。现在,我需要该参数来匹配我的对象,该对象具有我通过 Ajax 传递的确切属性。我在那里的某个地方犯了一个错误,我不确定这里的正确方法是什么......
If i set "@RequestBody String someString" it will return the parameters i pushed with ajax, but i won't be able to access that info with the getters 'cause it's a string. So i need an object to collect those values!
如果我设置了“@RequestBody String someString”,它将返回我用 ajax 推送的参数,但我将无法使用 getter 访问该信息,因为它是一个字符串。所以我需要一个对象来收集这些值!
采纳答案by JovanGacic
The answer was the Wrapper class. It couldn't assign values to it and threw error because i set attributes to "private". Setting them "public" solved it for me.
答案是 Wrapper 类。它无法为其分配值并抛出错误,因为我将属性设置为“私有”。将它们设置为“公开”为我解决了这个问题。
Can't believe that was the error...
不敢相信这是错误...

