Siva
Siva

Reputation: 3728

JSON to Javascript

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?

Upvotes: 1

Views: 247

Answers (3)

Jakob
Jakob

Reputation: 24360

Attempting to do anything with data.model[0].model_name before you know what data is sounds bad to me. And even if it just for testing something, eval won't really help you.

So, the first step in debugging this would be to alert the xmlHttp.responseText variable!

  • If it doesn't look like you expect it to do, then the server is the issue.

  • If it does look like you expect it to do, then you have some problem client side (or you've missed something about what's valid in json).

That being said, Felix's answer is probably the right way to go.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816262

You are creating a string that looks like JSON and pass this to json_encode, which is wrong. json_encode accepts an object or an array. So, create an array instead of a string:

$data = array();

while(($result_row = mysql_fetch_row($result))) {
    $data = array('model_name' => $result_row[1] . '(' . $result_row[2] .')');
}

echo json_encode(array('model' => $data));

Your JavaScript code looks ok.

Upvotes: 2

Pir Abdul
Pir Abdul

Reputation: 2334

try the following lines of codes var data = eval("(" + xmlHttp.responseText + ")"); alert(data.model[0].model_name)

Upvotes: 0

Related Questions