I'm not sure what's wrong with the way I'm doing this... I get a 400 Error saying it's a bad request, but I can't find anything wrong with my syntax.
我不确定我这样做的方式有什么问题...我得到一个400错误,说这是一个糟糕的请求,但我发现我的语法没有任何问题。
$.ajax({
url : '/my_project/rest/runs/1234?token=moo',
type : 'POST',
data: { job_position : JSON.stringify(38) },
contentType: 'application/json',
dataType: 'json',
success : function(html) {
}
});
The receiving controller:
接收控制器:
@RequestMapping(value="/runs/{userId}", method = RequestMethod.POST, consumes = {"application/json"})
public @ResponseBody boolean myMethod(@PathVariable String userId, @RequestParam("token") String authenticationToken, @RequestBody @Valid Long job_position){
return true;
}
3 个解决方案
#1
1
Your not actually sending JSON in your request, jQuery will convert your object to a query string. To prevent this stringify it yourself.
您实际上并未在请求中发送JSON,jQuery会将您的对象转换为查询字符串。为了防止这个字符串化自己。
$.ajax({
url : '/my_project/rest/runs/1234',
type : 'POST',
data: JSON.stringify({ job_position : 38, token: 'moo' }),
contentType: 'application/json',
dataType: 'json',
success : function(html) {
}
});
#2
2
Is your data part missing quotes? data: { "job_position" : JSON.stringify(38) }
您的数据部分是否缺少引号? data:{“job_position”:JSON.stringify(38)}
Just a thought.
只是一个想法。
#3
2
/my_project/rest/runs/1234?token=moo
- it's GET request sintax
/ my_project / rest / runs / 1234?token = moo - 这是GET请求sintax
makeurl : '/my_project/rest/runs/1234'
anddata: { "job_position" : JSON.stringify(38) , "token" : "moo"}
make url:'/ my_project / rest / runs / 1234'和data:{“job_position”:JSON.stringify(38),“token”:“moo”}
so full request look like
如此完整的请求看起来像
$.ajax({
url : '/my_project/rest/runs/1234',
type : 'POST',
data: { "job_position" : 38, "token" : "moo"},
contentType: 'application/json',
dataType: 'json',
success : function(html) {
}
});
#1
1
Your not actually sending JSON in your request, jQuery will convert your object to a query string. To prevent this stringify it yourself.
您实际上并未在请求中发送JSON,jQuery会将您的对象转换为查询字符串。为了防止这个字符串化自己。
$.ajax({
url : '/my_project/rest/runs/1234',
type : 'POST',
data: JSON.stringify({ job_position : 38, token: 'moo' }),
contentType: 'application/json',
dataType: 'json',
success : function(html) {
}
});
#2
2
Is your data part missing quotes? data: { "job_position" : JSON.stringify(38) }
您的数据部分是否缺少引号? data:{“job_position”:JSON.stringify(38)}
Just a thought.
只是一个想法。
#3
2
/my_project/rest/runs/1234?token=moo
- it's GET request sintax
/ my_project / rest / runs / 1234?token = moo - 这是GET请求sintax
makeurl : '/my_project/rest/runs/1234'
anddata: { "job_position" : JSON.stringify(38) , "token" : "moo"}
make url:'/ my_project / rest / runs / 1234'和data:{“job_position”:JSON.stringify(38),“token”:“moo”}
so full request look like
如此完整的请求看起来像
$.ajax({
url : '/my_project/rest/runs/1234',
type : 'POST',
data: { "job_position" : 38, "token" : "moo"},
contentType: 'application/json',
dataType: 'json',
success : function(html) {
}
});