duanli6834
2013-07-30 14:17
浏览 30
已采纳

PHP:得到了一个json解析错误

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); 

  ?>  

图片转代码服务由CSDN问答提供 功能建议

我想通过jQuery使用ajax将数据从数据库导入到我的javascript代码中,但我得到了一个json解析 错误,我真的不知道它的来源。 你可以帮帮我吗 ?

目标是在地图上构建建筑物,然后从数据库中获取几何元素,如坐标和形状参数。

JS文件,放置:

  $ .ajax({
 
 type:“GET”,
 url:“ajax_processor.php”,
  n dataType:“html”,
 
错误:function(msg,string){
 alert(“Error!:”+ string); 
} 
 
成功:function(returnData){
 \  n var data = $ .parseJSON(returnData); 
 
 for(var ID_geometryElement in data){
 addComp($ data [bldg],
 $ data [iZ],// zone其中是建筑物
  $ data [iType],//几何元素的类型
 $ data [x0],
 $ data [y0],//坐标左上角
 $ data [p],//几何参数
)  ; 
} 
} 
}); 
   
 
 

});

in PHP文件:

  try {
 $ bdd = new PDO('mysql:host = localhost  ; dbnam  e = building_geometry','admin','password'); 
} 
 
 $ reponse = $ bdd-> query('SELECT * FROM ID_GEOMETRYELEMENT'); 
 $ donnees = $ reponse-> fetch  ();  
 
标题('Content-Type:application / json');  
 echo json_encode($ response);  
 
?>  
   
  
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglu9896 2013-07-30 14:21
    已采纳

    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.

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题