PHP - Unable to send JSON correctly

user9993

I'm not sure why this isn't working, as a lot of the examples on the internet suggest doing it this way. Anyway, I have a SQL result that I've converted to JSON and now I'm trying to use that with Javascript.

json_encode($test, true); ?>

<script type="text/javascript">
var obj = (<?php echo $test; ?>);
alert(obj.toSource());
</script> 

This does not work and Chrome gives me an error of "illegal character" and the Javascript variable somehow displays some x-debug HTML from the PHP server: enter image description here

If I simple echo the JSON out to display on the webpage that works fine without any errors. What am I doing wrong?

ElmoVanKielmo

Do it like this:

$test = json_encode($test, true);

json_encode doesn't change the variable in place.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

unable to send json data via ajax

From Dev

Unable to send JSON object to Rest web service

From Dev

JSON PHP Not parsing correctly

From Dev

NODEJS unable to send json formatted response?

From Dev

Unable to send email using core php

From Dev

Unable to send data to php from jquery ajax?

From Dev

php send form doesn't work correctly

From Dev

Unable to send json from client to php and recieve json response back from php

From Dev

Get correctly formatted json from php to javascript?

From Dev

Unable to echo json data in PHP

From Dev

send post json with php (curl)

From Dev

unable to send email using php on wamp server

From Dev

Unable to send JSON data to MVC controller

From Dev

Unable to send data to php from jquery ajax?

From Dev

Send json from javascript to php

From Dev

PHP Curl: Unable to send extra header

From Dev

Unable to update json file in PHP

From Dev

Php mail function unable to send HTML

From Dev

php send form doesn't work correctly

From Dev

Unable to send push notification to ios using php

From Dev

Unable to send json from client to php and recieve json response back from php

From Dev

Unable to send data to php on select box change

From Dev

How to correctly send a json to a RestController using ajax?

From Dev

Unable to send mail using php mail() function

From Dev

How to decode a json correctly in php?

From Dev

Send JSON with PHP

From Dev

PHP Array not formatting correctly with JSON

From Dev

Send JSON form JavaScript to PHP

From Dev

How to correctly format JSON using PHP