简体   繁体   中英

Return Type for JsonArray from Action to JSP having multiple JSONArray Objects?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM