I have a PHP variable which contains a JSON
object in string format.
<?php
$url = "http://ip-api.com/json";
$phpObj = file_get_contents($url);
?>
Javascript:
<script>
var obj = "<?php echo $phpObj; ?>";
</script>
When I try to echo a PHP variable in javascript
, I get the following error:
Uncaught SyntaxError: Invalid or unexpected token.
I've tried using json_encode
, but it gives me the same error.