I'm trying to use JSON, I'm getting the information from the server so I've written this PHP file:
include("db_connect.php");
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query("SET NAMES utf8");
$query = "SELECT * FROM models WHERE models.product_id='".$product_selected."'";
$result = mysql_query($query);
$json_object = "{ \"model\": [";
while($result_row = mysql_fetch_row($result)) {
$json_object .= " {\"model_name\" : \"".$result_row[1]."(".$result_row[2].")";
$json_object .= "\"},";
}
$json_object = substr($json_object,0,strlen($json_object)-1);
$json_object .= " ]};";
echo json_encode($json_object);
?>
The output of the PHP file is in JSON format like this:
{ "model":
[
{"model_name" : "xxxxx "},
{"model_name" : "xxxxx "},
{"model_name" : "link2(U)"},
{"model_name" : "xxxxx)"}
]
};
But i'm getting this response in Ajax like:
var my_JSON_object = {};
var xmlHttp = createXmlHttpRequestObject();
try {
xmlHttp.open("GET", "ajaxproduct_new.php?product_id=" product_id,true);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
my_JSON_object = JSON.parse( xmlHttp.responseText );
alert(my_JSON_object.model[0].model_name);
}
}
xmlHttp.send(null);
} catch (e){
alert("Can't connect to server:\" e.toString());
}
But when I'm doing alert of my_JSON_object.model[0].model_name it showing the error as
my_JSON_object.model is undefined.
Why is it coming like this? I have tried all the stuffs. Can any one please tell me?
;is illegal in JSON.