Ajax does not return Json

0

Why am I no return on Ajax? It goes through the alert (over the for), but never enters the FOR. I'm not using any framework.

Servlet:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {

            recuperarValores(request, response);
        } catch (Exception ex) {
            Logger.getLogger(GetValores.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void recuperarValores(final HttpServletRequest request, final HttpServletResponse response) 
            throws IOException, Exception {  


            request.setCharacterEncoding("UTF-8");  
            //response.setCharacterEncoding("UTF-8");  
            response.setContentType("application/json");   
            PrintWriter out = response.getWriter();  

         JSONArray jsonArray = new JSONArray();
         JSONObject responseObj = new JSONObject();
        try{
            Escala_DAO e = new Escala_DAO();
        List<Escala> procedimentos = e.GetEscalaFunc(12998, "2018-01-01", "2018-01-03");
                     //JSONArray jsonArray = new JSONArray();  
                     for (Escala obj : procedimentos) {
                         JSONObject js = new JSONObject(); 

                          js.put("jsRe", obj.getRe()); 
                          js.put("jsEntra",obj.getEntrada());
                          js.put("jsSai", obj.getSaida());
                          System.out.println("**********"+obj.getSaida());//aqui imprime normal


                          jsonArray.put(js);  
                          }  
                     responseObj.put("jsonArray", jsonArray);
                     out.print(responseObj);
                     //out.print(responseObj.toString());

                      //pw.print(responseObj.toString());
                      //out.flush();

                 } catch (JSONException e) {  
        }



    }

JSP:

<script type="text/javascript" src="http://www.google.com/jsapi"></script><scripttype="text/javascript" src="js/timeline.js"></script>
    <link rel="stylesheet" type="text/css" href="css/timeline.css">


    <script type="text/javascript" src="//code.jquery.com/jquery-2.1.1.min.js"></script>
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script></head><bodyonload="drawVisualization();">
 <script type="text/javascript">
        var timeline;

         //-----------------------------------------------------------------------
            var queryObject = "";
            var queryObjectLen = "";

            var processed_temp = new Array();
            var processed_tensao = new Array();

             google.load("visualization", "1", {packages:["corechart"]});
             google.setOnLoadCallback(drawVisualization);

                //---------------------

                $.ajax({
                     dataType:'json',
                     url : './GetValores',
                     type : 'POST',                 

                    success: function(data) {

                        queryObject = eval('(' + JSON.stringify(data) + ')');
                        queryObjectLen = queryObject.jsonArray.length;

            },

                    error: function() {

                        alert("Ocorreu um erro na requisição ajax");
                    }
                });


                alert("em cima do for");
                for(var i=0;i<queryObjectLen;i++){ 

                    var inicio = +new Date(queryObject.jsonArray[i].jsEntra);
                    var fim = +new Date(queryObject.jsonArray[i].jsSai);
                    var content = "Jovani";
                    alert("DENTRO DO FOR !!!!!!!!!!!!!!!");
                    //data.push([inicio,fim,"teste"]);


                }
    
asked by anonymous 25.01.2018 / 23:42

1 answer

1

You are running an asynchronous call and trying to iterate through a return that you have not yet gotten, process the result inside the success callback.

    var timeline;

//-----------------------------------------------------------------------
var queryObject = "";
var queryObjectLen = "";

var processed_temp = new Array();
var processed_tensao = new Array();

 google.load("visualization", "1", {packages:["corechart"]});
 google.setOnLoadCallback(drawVisualization);

    //---------------------

    $.ajax({
         dataType:'json',
         url : './GetValores',
         type : 'POST',                 

        success: function(data) {

            queryObject = eval('(' + JSON.stringify(data) + ')');
            queryObjectLen = queryObject.jsonArray.length;


            alert("em cima do for");
            for(var i=0;i<queryObjectLen;i++){ 

                var inicio = +new Date(queryObject.jsonArray[i].jsEntra);
                var fim = +new Date(queryObject.jsonArray[i].jsSai);
                var content = "Jovani";
                alert("DENTRO DO FOR !!!!!!!!!!!!!!!");
                //data.push([inicio,fim,"teste"]);


            }
        },

        error: function() {

            alert("Ocorreu um erro na requisição ajax");
        }
    });
    
25.01.2018 / 23:58