Solved

problem with a json results

Posted on 2014-11-09
2
608 Views
Last Modified: 2014-11-09
hi
i am making an ajax call to get a data to present in in a morris chart. i return a json_encode. but it returns with a double quote, and i am not sure why it isn't working.

this is my ajax call:
        $.ajax({
            type: 'POST',
            url: 'functions/charts.php',
            data: {
                "action":"bardaily"
            },
            datatype: 'json',
            success: function (data) {
            
                console.log(data);

                
                    var donut = new Morris.Donut({
                    element: 'sales-chart',
                    resize: true,
                    colors: ["#3c8dbc", "#f56954", "#3ea847"],
                    data: data,
                    hideHover: 'auto'
                });
   

            },
            beforeSend: function () {
                $('form#myform p#statusp ').prepend("<p class='loadit' ></p>");
            },
            complete: function () {
                $("p.loadit").fadeOut();
            }
        });

Open in new window


and in my php i have for now this:
        $values = array(
            array( 'label' => "air" , 'value' => 12) ,
            array( 'label' => "light" , 'value' => 40),
            array( 'label' => "rest" , 'value' => 20 )
            );
        echo json_encode($values);

Open in new window


i don't get any error messages but the chart isn't there. when i compare the data i get from the php and the the expected data, the difference is with the double quote.

what am i missing here?
0
Comment
Question by:derrida
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40430994
Try this demo to see if it helps you any.
PHP-JSON-display.php
<?php 
$values = array(
	array( 'label' => "air" , 'value' => 12) ,
	array( 'label' => "light" , 'value' => 40),
	array( 'label' => "rest" , 'value' => 20 )
	);
$data = json_encode($values);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP JSON display</title>
</head>
<body>
<h1>PHP JSON display</h1>
<pre>
<?php echo $data."<br><br>"; ?>

<p id="demo"></p>
<script type="text/javascript">
var text = '<?php echo $data; ?>'

var obj = JSON.parse(text);

for(var ii=0;ii<3;ii++) {
document.getElementById("demo").innerHTML +=
obj[ii].label + " : " +
obj[ii].value + "<br>"
}
</script>

</pre>
</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 40431007
hi
first thanks

i took the variables how you extracted them to use it in the data section but now i get that : SyntaxError:
missing variable name
      for(var ii=0;ii < theData.length;ii++) {
in firebug.

theData is the obj.

this is the code i tried:
                    var donut = new Morris.Donut({
                    element: 'sales-chart',
                    resize: true,
                    colors: ["#3c8dbc", "#f56954", "#3ea847"],
                    data: [
                         for( var ii=0;ii < theData.length;ii++) {
                         {label: theData[ii].label , value: theData[ii].value},
                        }
                    ],
                    hideHover: 'auto'
                });

Open in new window


the expected data section should look like this:
    var donut = new Morris.Donut({
        element: 'sales-chart',
        resize: true,
        colors: ["#3c8dbc", "#f56954", "#3ea847"],
        data: [
            {label: "this is label one", value: 12},
            {label: "this is label two", value: 30},
            {label: "this is label three", value: 20}
        ],
        hideHover: 'auto'
    });

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question