Jquery 如何在服务器端修改CORS请求(来自IE8或IE9)-以启用Spring绑定
客户端使用IE8或IE9向使用Spring3.1的my domain发出CORS请求,就像这样Jquery 如何在服务器端修改CORS请求(来自IE8或IE9)-以启用Spring绑定,jquery,spring,internet-explorer-9,cors,Jquery,Spring,Internet Explorer 9,Cors,客户端使用IE8或IE9向使用Spring3.1的my domain发出CORS请求,就像这样 if ($.browser.msie && window.XDomainRequest) { var xdr = new XDomainRequest(); xdr.open("post", "http://example.com/corstest/testPost"); xdr.onload = function() { var dom =
if ($.browser.msie && window.XDomainRequest)
{
var xdr = new XDomainRequest();
xdr.open("post", "http://example.com/corstest/testPost");
xdr.onload = function() {
var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.async = false;
dom.loadXML(xdr.responseText);
$("#corsdiv").html(xdr.responseText);
};
xdr.send("param1=abc");
return false;
}
else
{
$.ajax({
url: "http://example.com/corstest/testPost",
type: "POST",
data: "param1=abc",
success: function( data ) {
$("#corsdiv").html(data);
},
});
return false;
}
在服务器上,在控制器中:
@RequestMapping(value = "/testPost", method = RequestMethod.POST)
public @ResponseBody String testPost(@RequestParam Map<String,String> body, Model model, HttpServletRequest request)
{
logger.info("Content-type: " + request.getContentType() + ";" + request.getContentLength() + ";" + request.getParameter("param1"));
logger.info("body:" + body.toString() );
return "Success";
}
我知道对于IE8/9 CORS请求,请求的内容类型头只支持text/plain。(参考资料:)
据我所知,这就是Spring不绑定来自IE8/IE9的请求的原因——因为它期望application/x-www-form-urlencoded作为内容类型
我使用了一个过滤器来截获传入的请求,并将所有请求的ContentType设置为“application/x-www-form-urlencoded;charset=UTF-8”(现在用于测试目的)
参考,我将HttpServletRequest包装在HttpServletRequestWrapper中,它覆盖getContentType、getCharacterEncoding、getHeader、getHeaderNames和getHeaders方法,并将其传递给过滤器上的chain.doFilter()方法
我得到的结果是:
For IE9:
INFO : Content-type application/x-www-form-urlencoded; charset=UTF-8;10;null
INFO : body:{}
For Firefox:
INFO : Content-type: application/x-www-form-urlencoded; charset=UTF-8;10;abc
INFO : body:{param1=abc}
装订不牢固
我的问题是:
由于servlet容器将内容类型视为text/plain,因此它不会将请求主体解析为参数映射。Spring会找到一个空的参数映射,因此不会绑定数据。我认为Spring不会提供现成的解决方案。您可以沿着的留置权创建一个过滤器。我认为Spring没有提供现成的解决方案。您可以沿着的留置权创建一个过滤器。这个问题不应该转到rentacoder或oDesk吗?我希望获得一些现有解决方案/尝试的参考,或者指向可能的解决方案的指针,如果不是所有代码的话。在google/SO搜索中,我没有找到任何关于这个问题的参考资料。这个问题不应该问rentacoder或oDesk吗?我希望获得一些关于现有解决方案/尝试的参考资料,或者指向可能的解决方案的指针,如果不是所有代码的话。在google/SO搜索中,我没有找到任何关于这个问题的参考资料。我已经有了一个类似链接上的过滤器。没有这个CORS请求(在任何浏览器上)都不会工作。我已经有了一个过滤器,就像链接上的过滤器一样。没有这个CORS请求(在任何浏览器上)都不会工作。
For IE9:
INFO : Content-type application/x-www-form-urlencoded; charset=UTF-8;10;null
INFO : body:{}
For Firefox:
INFO : Content-type: application/x-www-form-urlencoded; charset=UTF-8;10;abc
INFO : body:{param1=abc}