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? Link to comment https://forums.phpfreaks.com/topic/213587-how-return-json-object-not-string-to-ajax/ 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. Link to comment https://forums.phpfreaks.com/topic/213587-how-return-json-object-not-string-to-ajax/#findComment-1111778 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! Link to comment https://forums.phpfreaks.com/topic/213587-how-return-json-object-not-string-to-ajax/#findComment-1111865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.