0

I have went through many solutions 1 but not getting json in java script getting an error.In my myfile.php file it contains

<?php
 ................
print json_encode($data, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
<html>
    <head>
         <script type="text/javascript" src="jquery.min.js"></script>
         <script type="text/javascript" src="example2.js"></script>
        <script>
           var json= loadJsonFromPHP(<?php echo json_encode($data) ?>);
           //var json= <?php echo json_encode($data) ?>;
        </script>
    </head>
    <body></body>
</html>

In my example2.js file

var loadJsonFromPHP = function(json) {
    console.log(json);
}

function init(){
  // init data
  var json =loadJsonFromPHP(json);
 }

getiing an error Cannot read property 'id' of undefined at console.log(json);,I am new to PHP ,and have no idea where am i getting wrong,

i have tried with .getJson in my js but getting error ReferenceError: $ is not defined

function init(){
      // init data
var json=$.getJSON('http://localhost/myfile.php', function(data) {
    console.log(data);
});

}

Stuck completely,any help,thanks.

9
  • 2
    have you included jquery.js ? Commented Dec 10, 2013 at 13:00
  • I have included jquery.min.js file. Commented Dec 10, 2013 at 13:07
  • I think you have to drop this function and keep : var json = <?php echo json_encode($data) ?>; access to json as a regular object. ie: var l = json.l, var i = json.i, etc... Commented Dec 10, 2013 at 13:09
  • then you need to $.parseJSON() in your second example, cause the variable json is string at the moment. Commented Dec 10, 2013 at 13:57
  • @VaheShadunts same error ReferenceError: $ is not defined ,thanks Commented Dec 10, 2013 at 14:10

5 Answers 5

0

Your myfile.php should only contan echo json_encode($data)

0

In "myfile.php" you can do something like this...

<?php
$array = array(
   "success" => true,
   "message" => "Greetings from the Server!"
);
echo json_encode($array);
?>

In your HTML file...

<html>
<head>
<script src='js/jquery.js'></script> <!-- your path to jquery goes here -->
</head>
<body>

<div id="target">Message from Server will appear here</div>

<script type="text/javascript">
    // On ready 
    $(function() {
    $.ajax({
        url: 'myfile.php',
        // Before sending the request, show a loading message
        beforeSend: function(){
            $("#target").html("Waiting for response...");
        }
    })
    .done(function(response){
        // Write the response message to the target div
        $("#target").html(response.message);
    });
</script>

<body>
</html>

And that should really do it.

You can reference the jQuery documentation for .ajax here: http://api.jquery.com/jQuery.ajax/

0

In javascript you need to parse the JSON string:

var jsonobj = JSON.parse('<?php echo json_encode($data) ?>');
1
  • JSON.parse is not part of jQuery. Are you sure your jQuery library is properly included? Commented Dec 10, 2013 at 13:24
0

In http://localhost/myfile.php make sure you have:

//You cannot have any echo here you must have clean json-string.
$json = json_encode( $data );
if( json_last_error() === JSON_ERROR_NONE ){
    http_response_code(200);//ok
    header('Content-type: application/json');
    die( $json );
}else{
    http_response_code(500);//server error
    die( 'Cannot encode $data.' );
}
0
0

Finally XMLHttpRequest worked out for me in example2.js ,hope it will help.

request = new XMLHttpRequest( );
 request.open("GET", "my.php", false);
 request.send(null);
  var json = eval ("(" + request.response + ")");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.