0

Hi guys I am fairly new to AJAX.Right now I'm trying to develop a live graph by getting bunch of data continuously.However I am stuck on passing the data from servlet after calling the post function using ajax

JSP

<div id="chartContainer" style="height: 300px; width: 100%;"></div>

JS

  function requestData() {
         $.ajax({
                url: "myURL", 
                type: "POST",
                success: function() {
                    //Check if any notifications are returned - if so then display alert
                    alert("success");
                },
                error: function(){
                    //handle any error 
                    alert("Error");
                  }
            });

        }

        setInterval(function(){
            requestData(),
            updateChart()
        }, updateInterval);

After calling AJAX, it will call the post function below.

    protected void _doPostGraphData(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

            ArrayList<String> arrayCurrency = new ArrayList<String>();
            select.info info = new select.info();
            ArrayList<select.Rates> rates = info.caseGetRates();
            arrayCurrency.add(rates.get(0).getBid());
            arrayCurrency.add(rates.get(0).getAsk());

            JSONObject data = new JSONObject();
            data.put("data",arrayCurrency);
            response.setContentType("application/json; charset=UTF-8");
    }

Right now I am stuck trying to pass my data back to JavaScript. Would love some guidance on how to get the data from post =).

1 Answers1

0
response.setContentType("application/json; charset=UTF-8");
// Get the printwriter object from response to write the required json object to the output stream 
PrintWriter out = response.getWriter(); 
out.print(data);
out.flush();
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
  • hi really thx for helping but I am still blur about it. Even i put those line of codes at my post function.How is my js supposed to retrieved the json object.Sorry for asking noob questions – user3539966 Sep 30 '16 at 19:06
  • change your success function `success:function(data, textStatus, jqXHR ){ // your data will have your result you want. }` – Sunil B N Oct 01 '16 at 08:23
  • I dont know what to say. You definitely save a soul here bro. Thx much for the help. Have a nice day (Y) =) – user3539966 Oct 01 '16 at 13:42