Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
2  
have you included jquery.js ? –  Vahe Shadunts Dec 10 '13 at 13:00
    
I have included jquery.min.js file. –  Aashu Dec 10 '13 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... –  Louis XIV Dec 10 '13 at 13:09
    
then you need to $.parseJSON() in your second example, cause the variable json is string at the moment. –  Vahe Shadunts Dec 10 '13 at 13:57
    
@VaheShadunts same error ReferenceError: $ is not defined ,thanks –  Aashu Dec 10 '13 at 14:10

5 Answers 5

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

share|improve this answer

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/

share|improve this answer

In javascript you need to parse the JSON string:

var jsonobj = JSON.parse('<?php echo json_encode($data) ?>');
share|improve this answer
    
JSON.parse is not part of jQuery. Are you sure your jQuery library is properly included? –  Raymond Elferink Dec 10 '13 at 13:24

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.' );
}
share|improve this answer
    
didnt pass the object,thanks. –  Aashu Dec 10 '13 at 13:52
up vote 0 down vote accepted

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 + ")");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.