I\'m not getting response as JSON type data from server.
I\'m using JSON plugin.
jQuery( \"#dialog-form\" ).dialog({
autoOpen: false,
heigh
A dataType : 'json'
is used by jQuery Ajax to specify a data type that is expected to return by the success
callback function when the action and result is executed, and a response returned from the server.
dataType
(default: Intelligent Guess (xml
,json
,script
, orhtml
))Type:
String
The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).
The URL should correctly point to the action mapping. Assume it will be in the default namespace, otherwise you should modify URL and mapping to add the namespace
attribute.
Returning json
result type is not needed if you build the JSONObject
manually. You can return text as stream result then convert a string to JSON if needed.
struts.xml
:
text/html
stream
Action:
public class PartAction extends ActionSupport {
public class SearchResult {
private String col1;
private String col2;
public String getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public String getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
public SearchResult(String col1, String col2) {
this.col1 = col1;
this.col2 = col2;
}
}
private InputStream stream;
//getter here
public InputStream getStream() {
return stream;
}
private List findList = new ArrayList<>();
public List getFindList() {
return findList;
}
public void setFindList(List findList) {
this.findList = findList;
}
private String list() {
JSONObject jo = new JSONObject();
try {
for (SearchResult part : findList) {
jo.put("col1", part.getCol1());
jo.put("col2", part.getCol2());
}
System.out.println("--------->:"+jo.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return jo.toString();
}
@Action(value="part", results = {
@Result(name="stream", type="stream", params = {"contentType", "text/html", "inputName", "stream"}),
@Result(name="stream2", type="stream", params = {"contentType", "application/json", "inputName", "stream"}),
@Result(name="json", type="json", params={"root", "findList"})
})
public String finder() {
findList.add(new SearchResult("val1", "val2"));
stream = new ByteArrayInputStream(list().getBytes());
return "stream2";
}
}
I have placed different results with result type and content type to better describe the idea. You could return any of these results and return JSON object either stringified or not. The stringified version requires to parse returned data to get the JSON object. You can also choose which result type better serializes to suit your needs but my goal was to show that if you need to serialize the simple object then json plugin is not necessary to get it working.
References: