I am trying to perform an ajax request using the jsonp data type due to cross domain issues in a clustered environment.
我正在尝试使用jsonp数据类型执行ajax请求,因为群集环境中存在跨域问题。
I can make jsonp requests to methods mapped with no @RequestBody parameters, but when I do try to implement a RequestMapping with a @RequestBody parameter I get a 415 Unsupported Media Type error.
我可以对没有@RequestBody参数映射的方法发出jsonp请求,但是当我尝试使用@RequestBody参数实现RequestMapping时,我得到415 Unsupported Media Type错误。
Usually when I get this problem it is due to some property not mapping correctly between the json object posted and the Java object it is mapped to in Spring. But the only discrepency I can find is that using jsonp it adds a parm named callback and one with the name of an underscore "_"
通常当我遇到这个问题时,由于某些属性在发布的json对象和它在Spring中映射到的Java对象之间没有正确映射。但我能找到的唯一不同之处是使用jsonp它会添加一个名为callback的parm和一个名为下划线“_”的parm。
So I added the tag @JsonIgnoreProperties(ignoreUnknown = true) to my Java object and figured that should solve that, however it is still throwing this error.
所以我将标记@JsonIgnoreProperties(ignoreUnknown = true)添加到我的Java对象中,并认为应该解决这个问题,但它仍然会抛出此错误。
Is there anything else I need to do?
还有什么我需要做的吗?
EDIT: I am now seeing this stack trace in the debug log output from Spring: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported
编辑:我现在在Spring的调试日志输出中看到此堆栈跟踪:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型'application / octet-stream'
$.ajax({
url : 'http://blah/blah.html',
data : { abc : '123' }, (I also tried to JSON.stringify the object but no difference)
dataType : 'jsonp',
success : function(response) {
alert('ok '+JSON.stringify(response));
},
fail : function(response) {
alert('error'+JSON.stringify(response));
}
});
The Spring controller is:
Spring控制器是:
@RequestMapping({ "blah/blah" })
@ResponseBody
public ReturnObject getBlahBlah (@RequestBody MyObject obj) throws Exception {
}
The parameter object is:
参数对象是:
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyObject {
private String abc;
// getter and setter for abc auto generated by MyEclipse
}
I have a breakpoint on the Controller method which is never hit.
我在Controller方法上有一个断点,它永远不会被击中。
1 个解决方案
#1
4
JSONP means that jQuery will create a <script>
element with src
pointing to your controller URL.
JSONP意味着jQuery将创建一个
As you can see, this approach doesn't allow you to pass any data in request body, all data should be passed in URL as query parameters. data : { abc : '123' }
means that abc=123
is added to the URL.
如您所见,此方法不允许您在请求正文中传递任何数据,所有数据都应作为查询参数在URL中传递。 data:{abc:'123'}表示将abc = 123添加到URL。
At controller side you need to use either @RequestParam
(to bind inidividual parameters) or @ModelAttribute
(to bind multiple parameters into an object):
在控制器端,您需要使用@RequestParam(以绑定单独的参数)或@ModelAttribute(将多个参数绑定到对象中):
public ReturnObject getBlahBlah (@RequestParam("abc") int abc) throws Exception { ... }
#1
4
JSONP means that jQuery will create a <script>
element with src
pointing to your controller URL.
JSONP意味着jQuery将创建一个
As you can see, this approach doesn't allow you to pass any data in request body, all data should be passed in URL as query parameters. data : { abc : '123' }
means that abc=123
is added to the URL.
如您所见,此方法不允许您在请求正文中传递任何数据,所有数据都应作为查询参数在URL中传递。 data:{abc:'123'}表示将abc = 123添加到URL。
At controller side you need to use either @RequestParam
(to bind inidividual parameters) or @ModelAttribute
(to bind multiple parameters into an object):
在控制器端,您需要使用@RequestParam(以绑定单独的参数)或@ModelAttribute(将多个参数绑定到对象中):
public ReturnObject getBlahBlah (@RequestParam("abc") int abc) throws Exception { ... }