简体   繁体   中英

How to retrieve the data returned by an AJAX request

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.

Here we go:

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

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


                }
            });

Hope it helps;)

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

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

Try This

$(function(){

callMeForAJAX();

});

function callMeForAJAX(){
$.post( "callMeForAJAX.jsp", function(data){
 if(data.length > 0)
 console.log(data);
 else
 console.log("DATA NULL");  
});
}

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