Java 如何在Struts 2 Action类中接收json请求数据,即';是由jquery的getJSON()发送的吗?
我正在创建一个基于Struts 2的web应用程序,其中我希望通过jQuery的Java 如何在Struts 2 Action类中接收json请求数据,即';是由jquery的getJSON()发送的吗?,java,jquery,json,struts2,Java,Jquery,Json,Struts2,我正在创建一个基于Struts 2的web应用程序,其中我希望通过jQuery的getJSON()将包含JSON数据的请求发送到一个Action类。发生了这么多事情 但是,我的Action类无法接收JSON数据。任何努力都会事先得到赞赏 $.getJSON("getPaycells", { level: $(this).val(), ajax: 'true' }, function(j){}); struts.xml中的元素: 应用程序/json 行动类: public
getJSON()
将包含JSON数据的请求发送到一个Action类。发生了这么多事情
但是,我的Action类无法接收JSON数据。任何努力都会事先得到赞赏
$.getJSON("getPaycells", {
level: $(this).val(),
ajax: 'true'
}, function(j){});
struts.xml中的
元素:
应用程序/json
行动类:
public class PayMatrix extends ActionSupport
{
private Map data = new HashMap();
public Map getData()
{
return data;
}
public void setData(Map data)
{
this.data = data;
}
public String execute()
{
System.out.print("MYDATA-"+data);
return null;
}
}
网站上有一篇很好的文章 尝试将
enableSMD
设置为true
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
如果请求没有特定属性,json拦截器将丢弃该请求
我自己通过在
org.apache.struts2.json.JSONInterceptor
intercept方法上设置一些断点来解决我的问题,这将帮助我解释为什么intercept不适用于我的请求。它不是json数据。要开始使用Ajax,请阅读
$.ajax({
url: "writeJSON.action",
data: data1,
dataType: 'json',
contentType: 'application/json',
type: 'POST',
async: true,
success: function (res) {
....
}
});