dagogo Posted October 22, 2015 Share Posted October 22, 2015 I am doing something i don't understand how. written a php code in O.O.P and the value gotten from it are objects. but i want to convert this O.O.P object to JSON data to be used in by javascript. so I converted my converted my objects to array on the php end. the try to use the json_encode function on it the script just keep returning errors. so i tried using a function i scope out, it worked but the js keeps on rejecting the data. Below is the JS file var ajax = new XMLHttpRequest(); ajax.open('GET','user.php',true); ajax.setRequestHeader("Content-type","application/json"); ajax.onreadystatechange = function(){ if(ajax.readyState == 4 && ajax.status ==200){ var data = JSON.parse(ajax.responseText.trim()); console.log(data); console.log(data[username]); } } ajax.send(); I am doing something i don't understand how. written a php code in O.O.P and the value gotten from it are objects. but i want to convert this O.O.P object to JSON data to be used in by javascript. so I converted my converted my objects to array on the php end. the try to use the json_encode function on it the script just keep returning errors. so i tried using a function i scope out, it worked but the js keeps on rejecting the data. Below is the JS file var ajax = new XMLHttpRequest();ajax.open('GET','user.php',true);ajax.setRequestHeader("Content-type","application/json");ajax.onreadystatechange =function(){if(ajax.readyState == 4 && ajax.status ==200){var data = JSON.parse(ajax.responseText.trim());console.log(data);console.log(data[username]);}}ajax.send(); it will return this error "SyntaxError: JSON.parse: bad control character in string literal at line 1 column 129 of the JSON data" without the JSON.parse it return undefind fro the data.username console log. Below is the PHP SCRIPT //header("Content-type: application/json"); require_once 'core/init.php'; function array2json($arr){ /*if (function_exists('json_encode')) { echo "string"; return json_encode($arr); }*/ $pars = array(); $is_list = false; $keys = array_keys($arr); $max_length = count($arr) -1; if(($keys[0] == 0) and ($keys[$max_length]==$max_length)){ $is_list = true; for($i =0; $i<count($keys);$i++){ if($i!= $keys[$i]){ $is_list = false; break; } } } foreach ($arr as $key => $value) { if(is_array($value)){ if($is_list)$parts[]= array2json($value); else $part[] = '"'.$key.':'.array2json($value);} else{ $str=''; if(!$is_list)$str ='"'.$key.'"'.':' ; if(is_numeric($value))$str .= $value; elseif($value ===false)$str .= 'false'; elseif($value === true)$str .='true'; else $str .= '"'.addslashes($value). '"'; $parts[] = $str; } } $json = implode(',', $parts); if($is_list)return '['.$json.']'; return'{'.$json.'}'; } $user = new User(); $json = array(); if(!$user->is_LOggedIn()){ echo"false"; } else{ foreach ($user->data() as $key => $value) { $json[$key] = $value; //$json =json_encode($json,JSON_FORCE_OBJECT); //echo $json; } /*$details = '{"'.implode('", "', array_keys($json)).'"'; $data = '"'.implode('" "', $json).'"}'; die($details.' / '.$data);*/ $json = array2json($json); print $json; } PLEASE HELP ME OUT TO SORT THIS ERROR THANK YOU. HOPE A PLACE THIS IN THE Right THREAD Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2015 Share Posted October 22, 2015 to use the json_encode function on it the script just keep returning errors. Could you post the errors you are getting. Also could you tell us what is $user->data() is returning? (use print_r or var_dump to see contents). Quote Link to comment Share on other sites More sharing options...
Strider64 Posted October 22, 2015 Share Posted October 22, 2015 (edited) Also I think it would be easier to assemble the array (or output) first then use json_encode, for example: $output = json_encode($myArray); output($output); /* If there is an error then change the error to the proper code and output the */ /* error message via Ajax /JQuery. */ function error($output, $code = 500) { http_response_code($code); echo $output; } /* If everything validates OK then send success message to Ajax/jQuery */ function output($output) { http_response_code(200); echo $output; } Edited October 22, 2015 by Strider64 Quote Link to comment Share on other sites More sharing options...
dagogo Posted October 22, 2015 Author Share Posted October 22, 2015 Could you post the errors you are getting. Also could you tell us what is $user->data() is returning? (use print_r or var_dump to see contents). Warning</b>: Illegal string offset 'username' in <b>C:\Users\Ilamini\Desktop\xampp\htdocs\church-app\user.php</b> on line <b>55</b> vardump object(stdClass)#6 (10) { ["user_id"]=> string(1) "1" ["username"]=> string(11) "dagogodboss" ["password"]=> string(64) "eef4bc1edd0813b2525e1d5cbfeb0e331b9be1a7adc1fce6de64a52068412973" ["salt"]=> string(32) "jjWDzhU$%*" ["joined"]=> string(19) "2015-09-25 17:15:40" ["last_edit"]=> string(19) "0000-00-00 00:00:00" ["fullname"]=> string(25) "ilamini Ayebatonye Dagogo" ["group"]=> string(1) "1" ["home_address"]=> string(7) "maccoba" ["phone_number"]=> string(11) "08132841856" } Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2015 Share Posted October 22, 2015 What is line 55 of user.php? This will be why your javascript code cannot parse the json from the response because both the php error message and json encoded array are being returned. 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.