Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
Thread: JSON outputs as Undefined?
07-30-2013, 02:09 PM #1
- Join Date
- Apr 2013
- Thanked 0 Times in 0 Posts
JSON outputs as Undefined?
Hi, my code below converts a result into JSON fine as you can see from the link below but I am gettiing 'UNDEFINED' in my page
As it is an array does it need  somewhere?!
$stud_id = $_GET["trackid"];
$qr = "SELECT * FROM trialstracks WHERE trackid = $stud_id";
$row = mysql_fetch_array($res);
$stud_arr["trackid"] = $row["trackid"];
$stud_arr["trackname"] = $row["trackname"];
$stud_arr["trackdifficulty"] = $row["trackdifficulty"];
$stud_arr["trackcreator"] = $row["trackcreator"];
$stud_arr["tracktype"] = $row["tracktype"];
$stud_arr["trackbio"] = $row["trackbio"];
$stud_arr["trackpic"] = $row["trackpic"];
$stud_arr["trackvideo"] = $row["trackvideo"];
header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); // set the methods you want
header('Access-Control-Allow-Origin: *'); // this allows access from any domain
07-30-2013, 07:52 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,662 Times in 2,631 Posts
Since there's no error handling in here, there are plenty of opportunities to trigger undefined in PHP. You'll need to be more specific on the error.
Given the link you have though, there's no problem here. I see a JSON array as your results, so that says there's nothing wrong.
Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)PHP Code:
header('HTTP/1.1 420 Enhance Your Calm');
07-31-2013, 07:38 AM #3
- Join Date
- Jan 2010
- Behind the Wall
- Thanked 428 Times in 424 Posts
I’m not sure how you call that JSON in JS, but be aware that this JSON is an array, which only contains a single object, i.eAs it is an array does it need  somewhere?!
property_value = json[property_name]
however, according to your output the code above is not the one creating the JSON (no hint of a wrapping array).
additional note, PHP associative arrays translate into JS plain objects, not JS arrays.
The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.André Behrens, NY Times Software Developer