0

I want to import data from the database into my javascript code, using ajax through jQuery, but I got a json parse error and I really don't know what it does come from. Could you help me ?

The goal is to build buildings on a map, and i take the geometry elements such as coordinates and shape parameters from the database.

in the JS file, put:

 $.ajax({ 

  type: "GET",
  url: "ajax_processor.php",
  dataType : "html",

  error:function(msg, string){ 
     alert( "Error !: " + string );
  }

  success:function(returnData){ 

     var data = $.parseJSON(returnData); 

     for(var ID_geometryElement  in data) {
          addComp($data[bldg], 
                  $data[iZ], // zone where is the building
                  $data[iType], //type of the geometric element
                  $data[x0],
                  $data[y0],//coordinates top-left
                  $data[p], // geometric parameters
                  );
     }
   }
 });

});

in the PHP file:

try {
   $bdd = new PDO('mysql:host=localhost;dbname=building_geometry','admin','password');
}

 $reponse = $bdd->query('SELECT * FROM ID_GEOMETRYELEMENT');
 $donnees = $reponse->fetch(); 

  header('Content-Type: application/json'); 
  echo json_encode($response); 

  ?>  
5
  • 1
    What are you getting back from the ajax? Commented Jul 30, 2013 at 14:20
  • First of all you have to fix one thing in JS code. If you're writing for(var ID_geometryElement in data) { then you getting each item in ID_geometryElement variable, not in $data (and from where you get $ sign in JS var?). Commented Jul 30, 2013 at 14:23
  • What do you mean by that ? I want the ajax to execute the function addComp() and to fill the data with those it queried from the database, but I got an error before, in the json parsing. Commented Jul 30, 2013 at 14:24
  • Is your php program outputting anything else other than the output of a single call to json_encode()? What does the browser show in the http response data? Also, in your JS code, you appear to have a variable named data, but it looks like you're referring to it by the name $data. That looks like a bug. Commented Jul 30, 2013 at 14:25
  • you have a try without a catch.... Commented Jul 30, 2013 at 14:30

3 Answers 3

2

You're trying to json_encode the database statement handle your query returned. That is NOT something you can encode. The code should be

echo json_encode($donnees);
                 ^^^^^^^^--- the actual data

If you'd done even the most basic debugging, e.g. console.log(returnData) in JS, you'd have seen you're not getting anything valid returned from the script.

Sign up to request clarification or add additional context in comments.

2 Comments

Since that's my very first time in coding in javascript, i didn't know there was this debug tool. I'll try that, thanks !
Thanks, that works now using Firebug, I got the json parsed correctly.
0
dataType : "html",

must be

dataType : "json",

if you expect JSON in the response

EDIT: See http://api.jquery.com/jQuery.post/

dataType

Type: String

The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

2 Comments

that does nothing. datatype json tells jquery you're EXPECTING json back from the server, causing it to decode the string automatically. but OP's doing .parseJSON anyways, so type:html is 'ok'.
Oh yeah, I already changed that, but I have still the error in json parse
0

$response is just a cursor, you need to return the fetched data.

try {
   $bdd = new PDO('mysql:host=localhost;dbname=building_geometry','admin','password');
}

$reponse = $bdd->query('SELECT * FROM ID_GEOMETRYELEMENT');
$donnees = $reponse->fetch(); 

header('Content-Type: application/json'); 
echo json_encode($donnees); 

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.