someguy321 Posted September 16, 2010 Share Posted September 16, 2010 I have the following code and it always says it's a string! How do I make it see it as a json object? <?php //We'll return a json object from post request if ( !empty( $_POST ) ) { $arr = array ('status'=>1,'message'=>'Some message'); echo json_encode($arr); } else { ?> <input type="button" value="submit" id="submit" /> <script type="text/javascript" src="/scripts/jquery-1.4.2.js"></script> <script> var submit = document.getElementById( "submit" ); submit.onclick = function() { $.post( "test.php", function(data) { /* * This always prints as: * {"status":1,"message":"Some message"} * string * Code: undefined * Message: undefined */ alert( data + "\n" + typeof( data ) + "\nCode: " + data.status + "\nMessage: " + data.message ); } ); } </script> <? } ?> What do I do to make it a json object? Quote Link to comment Share on other sites More sharing options...
schilly Posted September 16, 2010 Share Posted September 16, 2010 You need to set the data response of the post call to json. http://api.jquery.com/jQuery.post/ I actually just did a post on this. See my blog if you want an example with code. Quote Link to comment Share on other sites More sharing options...
someguy321 Posted September 16, 2010 Author Share Posted September 16, 2010 You need to set the data response of the post call to json. http://api.jquery.com/jQuery.post/ I actually just did a post on this. See my blog if you want an example with code. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.