Json - driving me crazy! Yes, I've read through the docs

Clearly, I’m doing something wrong but I’ve been working on this all day and for some reason can’t get it to work. I’ve gone through http://api.jquery.com/jQuery.post/ backwards and forwards, any help is greatly appreciated!

jQuery:


$(function () {
	$('#vehicleModel').change(function() {
		$.post("ajax.php", { vehicleModel:$('#vehicleModel').val() },
		function(data){
			alert(data.optionValue);
			$('#vehicleYear option').remove();
			$.each(data,function(i,item) {
				$('#vehicleYear').append('<option value="'+item.optionValue+'">'+item.optionDisplay+'</option>');
			});
		}, "json");
	});
});

PHP:


  $Model_t = $_REQUEST['vehicleModel'];

  $results = mysql_query('select * from vehicle_applications where Model_t="' . $Model_t . '"');
  
  $json = array();
  
  while (is_resource($results) && $row = mysql_fetch_object($results)) {
      $Year = strtoupper($row->Year_t);
      $Model = strtoupper($row->Model_t);

      $json[] = '{optionValue: "'.$Model.'", optionDisplay: "'.$Year.'"}';
  }
  
  header('Content-type: application/json'); 
  echo '[' . implode(',', $json) . ']';
  die();

My response in firebug looks like:


[{optionValue: "FORD CAR", optionDisplay: "41-48"},{optionValue: "FORD CAR", optionDisplay: "32"},{optionValue: "FORD CAR", optionDisplay: "33-34"}]

I get nuthin’ from the alert but firebug says the json n/v is there.

Your JSON is not valid JSON.


Try this:


$json[] = '{"optionValue": "'.$Model.'", "optionDisplay": "'.$Year.'"}'; // notice the double quotes in the keys

See: http://json.org/

Instead of manually making json format, how about using json_encode() function:

http://us2.php.net/manual/en/function.json-encode.php

AAAARRRGGGGHHH!!! Thank you so much, I could have SWORN I tried formatting that way. The version I posted here just happened to be one of the last (of many) formatting efforts I made.