Reputation: 11
I am creating a Struts 2 based web application in which I want to send a request containing JSON data by getJSON()
of jQuery to an Action class. This much is happening.
However my Action class is unable to receive JSON data. Any effort is much appreciated in advance.
$.getJSON("getPaycells", {
level: $(this).val(),
ajax: 'true'
}, function(j){});
<action>
element in struts.xml:
<action name="getPaycells" class="pkg.ActionClassName">
<exception-mapping exception="java.lang.Exception" result="ERROR" />
<interceptor-ref name="json">
<param name="contentType">application/json</param>
</interceptor-ref>
<result type="json"/>
</action>
Action class:
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;
}
}
Upvotes: 1
Views: 4778
Reputation: 45475
There is a very good article at http://tech.learnerandtutor.com/send-json-object-to-struts-2-action-by-jquery-ajax/
Try to set enableSMD
to true
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
Also try to use some thing like below:
$.ajax({
url: "writeJSON.action",
data: data1,
dataType: 'json',
contentType: 'application/json',
type: 'POST',
async: true,
success: function (res) {
....
}
});
The json interceptor
discard your request if it dose not have specific properties.
I myself solved my issue by setting some break points at org.apache.struts2.json.JSONInterceptor
the intercept
method, this will help me why the interceptor not apply on my request.
Upvotes: 1