$.ajax({
url: ".../sendPropertyMessages.do",
type: "POST",
data: {
"buildingDetailIds": "1,2",
"propertyManagementMessage": {
"title": "test"
}
},
dataType: "json",
cache: false,
success: function(message) {
}
});
后台 propertyManagementMessage 这个对象 适配是 null buildingDetailIds 适配正常
@At
@Ok("json")
@AdaptBy(type=JsonAdaptor.class)
public Object test(@Param("propertyManagementMessage") PropertyManagementMessage propertyManagementMessage, @Param("buildingDetailIds") String[] buildingDetailIds){
try {
return Result.success("true");
} catch (Exception e) {
e.printStackTrace();
return Result.success("false");
}
}
要用 JSON.stringify
data: JSON.stringify({
"buildingDetailIds": "1,2",
"propertyManagementMessage": {
"title": "test"
}
}),
@wendal 这个试过了 还是 适配 null
@wendal {"buildingDetailIds":"1,2","propertyManagementMessage":{"title":"test"}}
@wendal public Object sendPropertyMessages(@Param("..")Map params ,@Param("propertyManagementMessage") PropertyManagementMessage propertyManagementMessage, @Param("buildingDetailIds") String[] buildingDetailIds) 这样吗 这样子 参数 适配的 全是 null param 也是
@wendal 还是 null ==!!!
请求的data被其他代码读取了??
@At
@Ok("json")
@AdaptBy(type=VoidAdaptor.class)
public Object test(HttpServletRequest req) {
log.info("body>>"+new String(Streams.readAndClose(req.getInputStream())));
}
@wendal 类型 Streams 中的方法 readAndClose(Reader)对于参数(ServletInputStream)不适用
@wendal test(PropertyManagementController.java:74) INFO - body>>
没有 信息
@wendal 额 其他的 参数 都能适配的
@wendal @At
@Ok("json")
@AdaptBy(type=VoidAdaptor.class)
public void test(HttpServletRequest req) throws IOException {
log.info("body>>"+new String(Streams.readAndClose(new InputStreamReader(req.getInputStream()))));
} 这个放在 大鲨鱼 NutzWk 也是没有输出的
@wendal 加了 @Param("..")Map params 是全部 请求参数都适配不到
打印一下body的长度和类型
public void test(HttpServletRequest req) throws IOException {
log.debug("content-length=" + req.getContentLength());
log.debug("content-type=" + req.getContentType());
}
@wendal http://localhost:8081/test/management/test >>>>>content-length=-1 content-type=null
@wendal $.ajax({
url: ".../sendPropertyMessages.do?buildingDetailIds=1,2",
type: "POST",
data: $('#loginForm').serialize(),
dataType: "json",
cache: false,
success: function(message) {
}
});
@At
@Ok("json")
public Object sendPropertyMessages(@Param("..") PropertyManagementMessage propertyManagementMessage, @Param("buildingDetailIds") String[] buildingDetailIds,HttpServletRequest req) 这样写 是可以的
@wendal 嗯 这样写 就是不行 欸 $.ajax({
url: "../sendPropertyMessages",
contentType: "application/json",
type: "POST",
data: JSON.stringify({
"propertyManagementMessage": {
"title": "test"
},
"buildingDetailIds":"1,2"
}),
dataType: "json",
cache: false,
success: function(message) {
}
});
@At
@Ok("json")
@AdaptBy(type=JsonAdaptor.class)
public Object sendPropertyMessages(@Param("propertyManagementMessage") PropertyManagementMessage propertyManagementMessage, @Param("buildingDetailIds") String[] buildingDetailIds,HttpServletRequest req)
content-length=-1
content-type=null
@wendal 不晓得啊 用的大鲨鱼的 NutzWK
@wendal 你 那 测试 都ok?
@wendal 好像是跨域请求的问题
<!DOCTYPE html>
MyHtml.html
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<script src="jquery-1.11.0.min.js"></script>
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
$("#button").click(function(){
// var data1 = $('#loginForm').serialize()
// var data = '{cms_article:{"title": "世贸公元一期花园洋房"}}';
var data = '{cms_article:{"title": "测试"},a:1}';
$.ajax({
url: "http://127.0.0.1:8080/NutzWk-bootstrap-3.3.x/platform/cms/article/sendPropertyMessages",
contentType: "application/json;charset=utf-8",
type: "POST",
// data: {buildingDetailIds: "1,2"},
// data: JSON.stringify({
//
// "cms_article": {
// "title": "test"
// },
//
// }),
data: data,
dataType: "json",
cache: false,
success: function(message) {
}
});
});
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
// String str = config.getInitParameter("AccessControlAllowOrigin");
// 表明它允许"http://xxx"发起跨域请求
httpResponse.setHeader("Access-Control-Allow-Origin",
config.getInitParameter("AccessControlAllowOrigin"));
/* httpResponse.setHeader("Access-Control-Allow-Origin",
config.getInitParameter("AccessControlAllowOrigin"));
*/ // 表明在xxx秒内,不需要再发送预检验请求,可以缓存该结果
httpResponse.setHeader("Access-Control-Allow-Methods",
config.getInitParameter("AccessControlAllowMethods"));
// 表明它允许xxx的外域请求
httpResponse.setHeader("Access-Control-Max-Age",
config.getInitParameter("AccessControlMaxAge"));
// 表明它允许跨域请求包含xxx头
httpResponse.setHeader("Access-Control-Allow-Headers",
config.getInitParameter("AccessControlAllowHeaders"));
chain.doFilter(request, response);
}
已拦截跨源请求:同源策略禁止读取位于 http://127.0.0.1:8080/NutzWk-bootstrap-3.3.x/platform/cms/article/sendPropertyMessages 的远程资源。(原因:CORS 头 'Access-Control-Allow-Origin' 不匹配 '*, *')。