NutzCN Logo
问答 JsonAdaptor 适配器问题
发布于 382天前 作者 qq_868910ac 392 次浏览 复制 上一个帖子 下一个帖子
标签:
	$.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");
		}
	}
30 回复

要用 JSON.stringify

				data: JSON.stringify({
						   "buildingDetailIds": "1,2",
						   "propertyManagementMessage": {
							  "title": "test"
						   }
						}),

@wendal 这个试过了 还是 适配 null

开Chrome的开发者工具,看看发送到后端的内容是啥

@wendal {"buildingDetailIds":"1,2","propertyManagementMessage":{"title":"test"}}

声明一下params参数,并打印一下它

@Param("..")Map params

@wendal public Object sendPropertyMessages(@Param("..")Map params ,@Param("propertyManagementMessage") PropertyManagementMessage propertyManagementMessage, @Param("buildingDetailIds") String[] buildingDetailIds) 这样吗 这样子 参数 适配的 全是 null param 也是

$.ajax({
	url : "....",
	type: "POST",
	contentType: "application/json",
	data : ....
});

请求的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)不适用

@qq_868910ac new InputStreamReader(req.getInputStream())

@wendal test(PropertyManagementController.java:74) INFO - body>>
没有 信息

所以,是适配器之前的某个东西把data都吃掉了

@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 也是没有输出的

其他参数? 你不是说全部都是null吗?

@wendal 加了 @Param("..")Map params 是全部 请求参数都适配不到

打印一下body的长度和类型

public void test(HttpServletRequest req) throws IOException {
	log.debug("content-length=" + req.getContentLength());
	log.debug("content-type=" + req.getContentType());
}

contentType: "application/json" 没生效的样子

@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)  这样写 是可以的

这种就不是发json到服务器咯

@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

web容器的问题?

@wendal 不晓得啊 用的大鲨鱼的 NutzWK

@wendal 你 那 测试 都ok?

是不是根本没走POST? Chrome的开发者工具里面的header贴出来

@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' 不匹配 '*, *')。

Accept
application/json, text/javascript, /; q=0.01
Accept-Encoding
gzip, deflate
Accept-Language
zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Content-Length
37
Content-Type
application/json;charset=utf-8
Host
127.0.0.1:8080
Origin
null
User-Agent
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0

搞那么多,原来要跨域

添加回复
请先登陆
回到顶部