Ajax does not return Json

Asked

Viewed 131 times

0

Why do I have no return in Ajax? It goes through Alert(on top of for), but never enters 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>
    <script type="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>

<body onload="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"]);


                }

1 answer

1


You are running an asynchronous call and trying to iterate on a return you have not yet obtained, process the result in on the callback of the success.

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");
        }
    });
  • Perfect! It worked, thank you very much!! I thought that there in the "Val" already placed all objects inside the "queryObject". Thank you very much for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.