1

I make an AJAX post request to get processed data from DB in form of "array" [value1, value2, value3,...,valueN] to later use it on a chartJS object.

AJAX Request:

$(document).ready($.post('callMeForAJAX.jsp', function(data){
    values = data;
    console.log(data);
}));

But it doesn't print anything on the console even though I can see the response on the "Network" tab on the Development Tools from Chrome.

How can I retrieve that data (isn't more than a String) to put it on the "data" parameter of the Chart object?

callMeForAJAX.jsp:

<%@ page contentType="text/xml;charset=UTF-8"%>
<%

  String data = "[";
  for(String s : InfoFacade.getData()){
      data += s+", ";
  }
  data += "]";
  response.getWriter().write(data);

%>

Edit: If relevant, I was using 1.2.x jQuery lib and now I have upgraded to 2.x without any changes.

Andrés Buitrago
  • 205
  • 1
  • 2
  • 9

3 Answers3

0

Here we go:

$.ajax({
                type: "POST",
                url: "/yourUrl",
                data: data,
                dataType: 'html',
                success: function (result) {

                        $("#yourIdContainerToShowResult").html(result);


                }
            });

Hope it helps;)

Husni Salax
  • 1,968
  • 1
  • 19
  • 29
  • Your solution seems to work as expected. Can you please correct my approach by saying what was wrong with the way I was trying to do things? Also: How can I store that data on a variable that is outside the AJAX request? It is undefined once I try to print it outside the callback. – Andrés Buitrago Apr 26 '16 at 14:03
  • @AndrésBuitrago 1. instead of $.post() you may use $.ajax() – Husni Salax Apr 26 '16 at 14:11
  • Yeah, I get that. According to jQuery docs that's just a shorter way to do it so I decided to use that way. On the other hand, can you help me with the second miniquestion about how to save that data on a global variable? Here's a jsfiddle so you can understand what I'm trying to do https://jsfiddle.net/zsjjcmjL/. It keeps saying values is undefined. – Andrés Buitrago Apr 26 '16 at 14:26
  • @AndrésBuitrago I looked to your code so, with that way you posting data as NULL as that you are getting values as undefined , first of all you must serialize your data as like this: var data = $("#yourOrderFormId").serialize(); if it helps pls, mark up my solution ;) – Husni Salax Apr 26 '16 at 14:33
  • This code ' $(document).ready(ajaxCall()) ' must be like this $(document).ready(function(){ ajaxCall(); }); – Husni Salax Apr 26 '16 at 14:41
0

Your $(document).ready() block is not correct. Try this:

$(document).ready(function(){
    $.post('callMeForAJAX.jsp', function(data){
        values = data;
        console.log(data);
    })
});
Rajesh Kumar
  • 471
  • 5
  • 14
0

Try This

$(function(){

callMeForAJAX();

});

function callMeForAJAX(){
$.post( "callMeForAJAX.jsp", function(data){
 if(data.length > 0)
 console.log(data);
 else
 console.log("DATA NULL");  
});
}
Shantanu Madane
  • 617
  • 5
  • 14