4

I have a JSONArray Which has 2 JSONArray Objects which i am returning from Action to JSP. But In JSP, it throws an error in the response received from Action. I am unable to track the issue however. Request you to please guide.

Action Code:

try
        {
            Class<EmployeePojo> objClass= EmployeePojo.class;
            Field[] methods = objClass.getDeclaredFields();
            columnJsonArrayObject=FormatDatesAndMethods.methodsData(methods);                                                       
            masterDataJsonArrayObject=new JSONArray();
            String query="from EmployeePojo";
            employeeList= factoryImplObject.searchByQuery(query);   
            if(employeeList!=null)
            {
                for(int j=0;j<methods.length;j++)
                {
                    for(int i=0;i<employeeList.size();i++)
                    {
                        masterDataColumnValuesJsonObject=new JSONObject();
                        if((employeeList.get(i)).getWorkshopId()!=null)
                        {
                            masterDataColumnValuesJsonObject.put(""+methods[0].getName()+"", employeeList.get(i).getId());  
                        }
                        else
                        {
                            masterDataColumnValuesJsonObject.put(""+methods[0].getName()+"", "");   
                        }
                        if((employeeList.get(i)).getWorkshopName()!=null)
                        {
                            masterDataColumnValuesJsonObject.put(""+methods[1].getName()+"", employeeList.get(i).getName());    
                        }
                        else
                        {
                            masterDataColumnValuesJsonObject.put(""+methods[1].getName()+"", "");   
                        }
                        if((employeeList.get(i)).getDivId()!=null)
                        {
                            masterDataColumnValuesJsonObject.put(""+methods[2].getName()+"", employeeList.get(i).getJivId());   
                        }
                        else
                        {
                            masterDataColumnValuesJsonObject.put(""+methods[2].getName()+"", "");   
                        }
                        if((employeeList.get(i)).getHqId()!=null)
                        {
                            masterDataColumnValuesJsonObject.put(""+methods[3].getName()+"", employeeList.get(i).getPlace());   
                        }
                        else
                        {
                            masterDataColumnValuesJsonObject.put(""+methods[3].getName()+"", "");   
                        }   
                        masterDataColumnValuesJsonObject.put("Old", "old");
                        masterDataJsonArrayObject.put(masterDataColumnValuesJsonObject);
                    }
                    break;
                }
                masterObject.put(0,columnJsonArrayObject);
                masterObject.put(1, masterDataJsonArrayObject);             
                if(masterObject!=null)
                {
                    out.write(masterObject.toString);   
                }
            }
        }
        catch(Exception e)
        {
        }

JSP Code

function values()
{
    var values=
    {
        url:"metaData.do?actionMethod=loadMasterData",
        handleAs:'json',
        content:parameter,
        load: function(response)
        {

        alert("working");

        },
        error: function(data)
        {
            alert("Error occured while fetching data");
        },
        timeout: 3000,
        sync: true                              
    };
    dojo.xhrPost(values);
}

The issue is i am not getting the correct response from Action and hence the error alert popsup in jsp Error occured while fetching data

4
  • Does the console provide any additional information about the error? (On a side note why are using pure js for ajax operations? It's really bothersome.) Dec 23, 2013 at 12:09
  • 1
    There is no error that i find in Action Class. But in JSP it prompts an Object Error.
    – sTg
    Dec 23, 2013 at 12:15
  • Oh you are using dojo. Never mind the side note. So it's an error on the side of the server? Try printing the stacktrace. Dec 23, 2013 at 12:25
  • Can you print out the masterObject.toString() with a System.out or log.debug() statement in your JSP?
    – Barryrowe
    Dec 23, 2013 at 16:47

1 Answer 1

1

I dont find any issues with the JSONArray Objects that you have returned from Action To JSP. Try Returning the JSONArrayObject through

out.println(masterObject.toString);

If still this is not working i feel like there is some junk data that is getting transferred from Action To JSP. Try debugging or Printing The System.out.println in your action method which may possibly let you know the data in your JSONArrayObject that is getting transferred from Action to JSP. If still issues? . If you are using some framework like Struts/Springs(Which you should have mentioned), check if the control is getting passed to your controller class, if the control is not getting passed. There should be some issue with your XML File from where the control passes(Eg: Struts.xml in case of Struts 2) to the Action class.

1
  • 1
    Genius you are. Thanks for all the possible stepss to track the issue. There was an issue with the Path mentioned in Struts-Config.xml for the Action Class. I rectified it and now i able to print the objects of JSONArrayObjects in my JSP Page.
    – sTg
    Dec 24, 2013 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.