Using variable from ajax response as json

Manyang

Im still newbie from ajax and Jquery, I will show my data base in graphic using c3.js, but I cant use my ajax response to javascript variable. This is my JSON response from response.php

[{"time":"2014-05-20 17:25:00","sensor1":"25","sensor2":"20","sensor3":"31","sensor4":"33","sensor5":"27"},{"time":"2014-05-20 17:26:00","sensor1":"26","sensor2":"23","sensor3":"33","sensor4":"31","sensor5":"23"}]

and this is my response.php

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "perkebunan";

//Connect to MySQL Server
$server = mysql_connect($dbhost, $dbuser, $dbpass);

//Select Database
mysql_select_db($dbname, $server) or die(mysql_error());
//build query
$query = "SELECT * FROM sensor";    
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
$results = array();
while($row = mysql_fetch_assoc($qry_result))
{
    $temp = array(
        'time' => $row['time'],
        'sensor1' => $row['sensor1'],
        'sensor2' => $row['sensor2'],
        'sensor3' => $row['sensor3'],
        'sensor4' => $row['sensor4'],
        'sensor5' => $row['sensor5']
    );
    array_push($results,$temp);
}
echo json_encode($results);
?>

and this is my database

my database

so please help me to make my json response to varible in javascript. this my trial index.php to make it posible.

<html>
<head>
    <!-- Load c3.css -->
    <link href="c3/c3.css" rel="stylesheet" type="text/css">
    <!-- Load d3.js and c3.js -->
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <script src="c3/c3.js"></script>
    <script src="jquery-2.2.3.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
    <div id="chart"></div>
<script language="javascript" type="text/javascript">  
function graphAjax(){
    var response;
     $.ajax({
        type : 'POST',
        url : 'ajax-example.php',
        dataType : 'json',
        data: { },
        success: function(data){
            response=data;
        }
    });
    var chart = c3.generate({
    bindto: '#chart',
    data: {
        json: {response
            /* I hope this variable response give result like this
            time: [2014-05-20 17:25:00, 2014-05-20 17:26:00, 2014-05-20 17:27:00, 2014-05-20 17:28:00, 2014-05-20 17:29:00],
            sensor1: [25, 26, 27, 28, 29],
            sensor2: [20, 23, 22, 25, 28],
            sensor3: [31, 33, 35, 30, 33],
            sensor4: [33, 31, 28, 25, 27],
            sensor5: [27, 23, 21, 19, 18],
            */
        }
    }
    });
}
</script>

   <input type='button' onclick='graphAjax()' value='Start'/>
</body>
<footer>
</footer>
</html>

thank you for your time and help.

user2314737

Since AJAX works asyncronously you need some kind of work-around. For instance, you can move the function that generates your chart inside the success callback function (and define what happens in case of an error adding

success: function(data){
            response=data;
            // generate chart here
        },
 error: function () {
            // do something
        },

Check also this extensive answer to How do I return the response from an asynchronous call?) or How to get the ajax response from success and assign it in a variable using jQuery?.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using ajax json response in an angular scope variable

From Dev

Ajax response using json

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get response text from ajax callback in a variable using ExtJS

From Dev

Extracting data from JSON response from AJAX using JavaScript

From Java

Using jQuery to build table rows from AJAX response(json)

From Dev

Creating a Javascript array using JSON response from ajax

From Dev

Get value from an odata property in a JSON response using ajax

From Dev

how to get JSON response from servlet to jsp page by using ajax

From Dev

AJAX : Getting two different JSON variable from php using ajax

From Dev

Dissecting/dividing "response" variable from console.log(response) using ajax

From Dev

get variable from json response

From Dev

How to select a variable from an AJAX response

From Dev

Ajax : empty json response when using Karma

From Dev

Setting Ajax response to drop down using json

From Dev

Display the JSON response received using AJAX

From Dev

Return response in JSON from AJAX command

From Dev

working with JSON data array from Ajax response?

From Dev

Javascript Reading JSON result from AJAX response

From Dev

AJAX Response Returns Undefined from JSON file

From Dev

Parse JSON object from Ajax response

From Dev

Extract json response to shell variable using jq

From Dev

No response from API using AJAX request

From Dev

Using jQuery to build table rows from Ajax response (not with static json data)

From Dev

closures with ajax, using variable from ajax

From Dev

Moving from using innerHTML for an AJAX response. (Code is not getting a response)

From Dev

Issues in storing a portion of an AJAX GET response (in JSON) into a string variable

From Dev

JS - Can not assign response from AJAX as custom variable

Related Related

  1. 1

    Using ajax json response in an angular scope variable

  2. 2

    Ajax response using json

  3. 3

    How to get the ajax response from success and assign it in a variable using jQuery?

  4. 4

    How to get the ajax response from success and assign it in a variable using jQuery?

  5. 5

    How to get response text from ajax callback in a variable using ExtJS

  6. 6

    Extracting data from JSON response from AJAX using JavaScript

  7. 7

    Using jQuery to build table rows from AJAX response(json)

  8. 8

    Creating a Javascript array using JSON response from ajax

  9. 9

    Get value from an odata property in a JSON response using ajax

  10. 10

    how to get JSON response from servlet to jsp page by using ajax

  11. 11

    AJAX : Getting two different JSON variable from php using ajax

  12. 12

    Dissecting/dividing "response" variable from console.log(response) using ajax

  13. 13

    get variable from json response

  14. 14

    How to select a variable from an AJAX response

  15. 15

    Ajax : empty json response when using Karma

  16. 16

    Setting Ajax response to drop down using json

  17. 17

    Display the JSON response received using AJAX

  18. 18

    Return response in JSON from AJAX command

  19. 19

    working with JSON data array from Ajax response?

  20. 20

    Javascript Reading JSON result from AJAX response

  21. 21

    AJAX Response Returns Undefined from JSON file

  22. 22

    Parse JSON object from Ajax response

  23. 23

    Extract json response to shell variable using jq

  24. 24

    No response from API using AJAX request

  25. 25

    Using jQuery to build table rows from Ajax response (not with static json data)

  26. 26

    closures with ajax, using variable from ajax

  27. 27

    Moving from using innerHTML for an AJAX response. (Code is not getting a response)

  28. 28

    Issues in storing a portion of an AJAX GET response (in JSON) into a string variable

  29. 29

    JS - Can not assign response from AJAX as custom variable

HotTag

Archive