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.