0

I am trying to get the Json Object data in JSP using Ajax. I have action class where I am putting data in Json Object, and returning success, as shown below:

public String execute()
{  
    JSONObject obj = new JSONObject();
    System.out.println("here inside action-------------");
    PersistenceService svc = PersistenceServiceImpl.getInstance();
    status = svc.getStatusByFileId(fileId);
    System.out.println("status is "+status);
    numRecords = svc.getNumRecordsByFileId(fileId);
    System.out.println("num records are "+numRecords);
    obj.put("status", status);
    obj.put("records", numRecords);
    System.out.print("json data is "+obj);
    return "SUCCESS";
}

My jsp ajax is:

$(document).ready(function(){
    $("#refresh").click(function(){
        var fileId=id;
        alert("ajax id is "+fileId);
        $.ajax({
            type:'post',
            url:'checkStatusAndNumRecs',
            dataType:'json',
            data:{fileId:fileId},
            success:function(data)
            {
                alert("data is :"+data); ->first alert
                var obj = jQuery.parseJSON(eval(data));
                alert("after parsing"); ->second alert
                $("#div1").html(obj.status);
                $("#div2").html(obj.records);
            },
            error:function(data)
            {
                $("#div1").html("It was a failure !!!");
            }
        });
    });
});

This is my struts.xml for this ajax action:

<action name="checkStatusAndNumRecs" class="com.mxui.checkStatusAndNumRecsAction" method="execute">
    <result name="SUCCESS">statusnrecs.jsp</result>
</action>

Problem is some times its going to success and many times its going to error, and var obj = jQuery.parseJSON(eval(data));->this line is not executing when ever it goes for success, alert before this line is coming but after this line i have put alert that is not showing.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
joee
  • 11
  • 4
  • 7

2 Answers2

0

The error is here

var obj = jQuery.parseJSON(eval(data));

Use either

var obj = jQuery.parseJSON(data); //probably better

or

var obj = eval(data); //older

not both. Also, you might to try

try {
  obj = jQuery.parseJSON(data);
} catch (e) {
  console.log(e);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • thankyou i tried both var obj = jQuery.parseJSON(data); and var obj = eval(data);but second alert is not executing at all – joee Nov 19 '13 at 05:33
  • is my action class method correct..where i am putting data in JSONObject – joee Nov 19 '13 at 05:56
  • @Frisch, can you help me in this after using var obj = jQuery.parseJSON(data); i alerted data,there nothing in that .. – joee Nov 19 '13 at 07:23
  • Sounds like you're not getting any reply with data; check that the url you're calling is correct. Maybe use fiddle2 or firebug? Also, I find console.log easier to use then alert. – Elliott Frisch Nov 19 '13 at 17:02
0

if u want to return json in struts, you struts.xml must set result's type = json,like this:

    <action name="checkStatusAndNumRecs" class="com.mxui.checkStatusAndNumRecsAction" method="execute">
        <result name="SUCCESS" type="json"></result>
    </action>
Vito
  • 1,080
  • 9
  • 19
  • thankyou i updated the struts,xml according to this then also same,second alert is not coming?? – joee Nov 19 '13 at 05:43
  • can you please tell me is the action class execute method i have posted is correct – joee Nov 19 '13 at 05:58