NutzCN Logo
精华 NutzWk中使用ajax请求后台无法获取到值
发布于 2738天前 作者 qq_55e8622e 1735 次浏览 复制 上一个帖子 下一个帖子
标签:

前端代码:
var paramData ={} ;
paramData['account']= "123456" ;
urlIn='${base}/home/getCurrLoadingChartData.nut';
$.ajax({
type : "POST",
url : urlIn,
dataType : "json",
data:JSON.stringify(paramData),
async : false,
success : function(datas) {
}
});

后台代码:
@At("/getJKInfo")
@AdaptBy(type = JsonAdaptor.class)
@Ok("json")
@RequiresAuthentication
public Object getJKInfo(@Param("account") String account) {
return homeService.getJKInfo("160614133542");
}
DEBUG进去account为null

请求监控信息:
Request URL:http://localhost:8080/platform/wdg/home/getJKInfo.nut
Request Method:POST
Status Code:200 OK
Response Headers
view source
Cache-Control:no-cache
Content-Type:application/json; charset=UTF-8
Date:Tue, 25 Oct 2016 14:05:33 GMT
Server:Jetty(9.2.17.v20160517)
Transfer-Encoding:chunked
X-Powered-By:nutz/1.r.58 <nutzam.com>
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:19
Content-Type:application/x-www-form-urlencoded;charset=UTF-8
Cookie:sid=f940dfed-f744-4e41-84e0-ae173db1ca52
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/platform/home
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
{"account":"12345"}:
6 回复
data:JSON.stringify(paramData),

你发的是json数据呢

@wendal 是发的JSON,这是从其他项目(基于nutz-1.r.55.jar)迁过来的,那项目没用NutzWk平台传JSON数据后台这样写是可以接收到的,老项目中前端统一传的JSON数据,后台下面这些写法都没问题:
获取JSON的对象 @Param("..") BusinessVO bsBaseInfo
获取JSON的list @Param("customerNo") List lstcustomerNo
获取JSON的数组@Param("contactIds")String[] contactIds
获取JSON的字符串@Param("customerNo")String customerNo
上面这些我写法中只要JOSN中设置了对应的key和value值后台都自动转化出来,对比老项目和NutzWk中的action类写法没什么区别……

json数据要配适配器哦

@AdaptBy(type=JsonAdapt.class)

@wendal 前端DEBUG信息
WDG.requestData= function(condition,urlIn){ condition = Object {account: "12345"}, urlIn = "/platform/wdg/home/getJKInfo.nut"
var jsonData = JSON.stringify(condition); jsonData = "{"account":"12345"}"
var rdata ={} ; rdata = Object {maxNeedCap: 0, needCap: 0, powerfactor: 0, lstMeterVO: Array[4]}
if(!urlIn){ urlIn = "/platform/wdg/home/getJKInfo.nut"
return rdata ; rdata = Object {maxNeedCap: 0, needCap: 0, powerfactor: 0, lstMeterVO: Array[4]}
}
$.ajax({
type : "POST",
url : urlIn, urlIn = "/platform/wdg/home/getJKInfo.nut"
data:JSON.stringify(condition), condition = Object {account: "12345"}
dataType : "json",
async : false,
success : function(datas) {
rdata = datas ; rdata = Object {maxNeedCap: 0, needCap: 0, powerfactor: 0, lstMeterVO: Array[4]}
}
});
return rdata ;
}

后台DEBUG为null
@AdaptBy(type = JsonAdaptor.class)
@At("/getJKInfo")
@Ok("json")
@RequiresAuthentication
public Object getJKInfo(@Param("account") String account) { accuount = null
return homeService.getJKInfo("160614133542");
}

wk的action就类上多了@Filters({ @By(type = PrivateFilter.class) })方法上多了@RequiresAuthentication这两我都去掉试过了没发现有影响,头大啊……

这样写:

   $.ajax({
      type: "POST",
      contentType: "application/json", // 设置正确的Content-Type,不然的话, 到达适配器前的任意req.getParam调用,都会消耗掉data,导致适配器无数据可读.
      url: urlIn,
      data: condition, // 传map就好了
      dataType: "json",
	  async : false,
      success : function(datas) {
           rdata = datas ; rdata = Object {maxNeedCap: 0, needCap: 0, powerfactor: 0, lstMeterVO: Array[4]}
      }
   });

@wendal 加了contentType可以取到了,感谢大神帮忙……

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