2017-03-21 37 views
1

Ich bekomme [object Object] Antwort in der Konsole, während ich einen vollen JSON erwarte.Wie bekomme ich rohe JSON-Daten mit Struts2 Ajax Anruf

Wie pro meine Action-Klasse:

public String getDensityData(){ 
    SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory"); 
    ProductDAO product = new ProductDAOImpl(sf); 
    List<DensityGroup> densityList = product.getDensity(); 
     Gson gson = new Gson(); 
     densityjson = gson.toJson(densityList); 
     System.out.println("JSON: "+densityjson); 
    return "success"; 
} 

struts.xml

<package name="json" namespace="/" extends="json-default"> 
    <action name="getJSON" class="com.bmm.action.ProductAction" method="getDensityData"> 
     <param name="noCache">true</param> 
     <param name="excludeNullProperties">true</param> 
     <param name="root">densityjson</param> 
     <result name="success" type="json">/product_master.jsp</result> 
</action> 
</package> 

JSP:

$.ajax({ 
    type: "POST", 
    url: '<s:url namespace="/" action="getJSON"/>', 
    data: "json", 
    success: function(data){ 
    console.log(""+data); 
    } 
}); 

Antwort

1

Wenn Sie aus der Antwort JSON erhalten erwarten, sollten Sie eine angeben dataType Eigentum. JSON ist ein Javascript Object, wenn Sie es drucken müssen, verwenden Sie JSON.stringify().

$.ajax({ 
    type: "GET", 
    url: '<s:url namespace="/" action="getJSON"/>', 
    dataType: "json", 
    success: function(data){ 
     console.log(JSON.stringify(data)); 
    } 
});