can not pass json array from PHP to JQuery -
i have been trying pass simple array php jquery. here have far relevant functions in html/js:
function myquery() { var xmlhttp = new xmlhttprequest(); var url = "sandbox.php"; xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { myfunc(xmlhttp.responsetext); } } xmlhttp.open("get", url, true); xmlhttp.send(); } function myfunc(response) { var arr = json.parse(response); document.getelementbyid("demco").innerhtml = arr[0]; }
for php have tried:
<?php $arr = [4,7,2]; echo json_encode($arr); ?>
and:
<?php $arr = array(4,7,2); echo json_encode($arr); ?>
according debugger fails @ json.parse(response) every time. wrong in code here? tried running array, [4,7,2], through http://json.parser.online.fr/ , got no issues. i'm @ wits end, me se!
edit:
from console: parse error in php on line x x line containing: $arr = [4,7,2]; uncaught exception: syntaxerror: json.parse: unable parse value:
error thrown @ line y, column 2 in myfunc(response) in http://... var arr = json.parse(response); called line z, column 4 in () in http://localhost/sandbox.html: myfunc(xmlhttp.responsetext);
kindly set output page header application/json clearly. required make output in json code.
<?php header('content-type:application/json'); $arr = array(4,7,2); echo json_encode($arr);
this make pure json output , readable javascript
Comments
Post a Comment