Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON to Javascript
    primarykey
    data
    text
    <p>I'm trying to use JSON, I'm getting the information from the server so I've written this PHP file: </p> <p> <pre><code> 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); </code></pre> <p>?></p> <p>The output of the PHP file is in JSON format like this:</p> <pre><code>{ "model": [ {"model_name" : "xxxxx "}, {"model_name" : "xxxxx "}, {"model_name" : "link2(U)"}, {"model_name" : "xxxxx)"} ] }; </code></pre> <p>But i'm getting this response in Ajax like:</p> <pre><code>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 &amp;&amp; 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()); } </code></pre> <p>But when I'm doing alert of <code>my_JSON_object.model[0].model_name</code> it showing the error as <code>my_JSON_object.model</code> is undefined.</p> <p>Why is it coming like this? I have tried all the stuffs. Can any one please tell me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload