从mysql获取存储为文本的json数据

时间:2014-05-12 22:22:31

标签: javascript php jquery mysql json

首先我有html - bootstrap标签:

<ul class="nav nav-tabs">
  <li class="active"><a href="#home" data-toggle="tab">Opsti podaci</a></li>
  <li><a id="mapa1" href="#mapa" data-toggle="tab">Mapa parcele</a></li>
  <li><a href="#dokumenti" data-toggle="tab">Dokumenti</a></li>
  <li><a href="#komentari" data-toggle="tab">Komentari</a></li>
</ul>

及以下:

<div class="tab-pane" id="mapa">
        <div id="myMap" style="height:500px;"></div>


     </div>

在数据库中我有这个: enter image description here

现在我编写这个jquery / ajax代码来将json存储为来自mySql数据库的文本:

$( document ).ready(function() {
$('a#mapa1').click(function (e) {

 $.ajax({
            url: "getMap.php",
            type: "POST",
            async: true, 
            data: { ajdi:ajdi}, //your form data to post goes here as a json object
            dataType: "html",

            success: function(data) {
            console.log(data);
                BlitzMap.setMap( 'myMap', true, 'data' ); 
                BlitzMap.init();   
            },  
            error: function(data) {
            console.log(data);
            }
        });
        });

但是当我点击#mapa1 notning happennd ...当我手动将此代码添加到console中的执行时我得到错误:SyntaxError: Unexpected end of input

这里有什么问题?我看不到!请帮忙!谢谢!

更新

 try {

     $result = $db->prepare("SELECT json FROM map WHERE id_parcele=:ajdi AND user_id=:user_id");
     $result->execute(array(':ajdi' => $_POST['ajdi'], ':user_id' => $user_id));
     $result = $result->fetchAll(); 

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    echo $result;
}

这让我产生了Array当我将echo $ result变为echo json_encode($ result)再次无法工作...如何获得纯文本?

使用print_r我得到这段代码:

Array
(
    [0] => Array
        (
            [json] => {"zoom":7,"tilt":0,"mapTypeId":"hybrid","center":{"lat":20.487486793750797,"lng":74.22363281640626},"overlays":[{"type":"polygon","title":"","content":"","fillColor":"#000000","fillOpacity":0.3,"strokeColor":"#000000","strokeOpacity":0.9,"strokeWeight":3,"paths":[[{"lat":"21.329035778926478","lng":"73.46008301171878"},{"lat":"21.40065516914794","lng":"78.30505371484378"},{"lat":"20.106233605369603","lng":"77.37121582421878"},{"lat":"20.14749530904506","lng":"72.65808105859378"}]]}]}
            [0] => {"zoom":7,"tilt":0,"mapTypeId":"hybrid","center":{"lat":20.487486793750797,"lng":74.22363281640626},"overlays":[{"type":"polygon","title":"","content":"","fillColor":"#000000","fillOpacity":0.3,"strokeColor":"#000000","strokeOpacity":0.9,"strokeWeight":3,"paths":[[{"lat":"21.329035778926478","lng":"73.46008301171878"},{"lat":"21.40065516914794","lng":"78.30505371484378"},{"lat":"20.106233605369603","lng":"77.37121582421878"},{"lat":"20.14749530904506","lng":"72.65808105859378"}]]}]}
        )

)

但我需要得到这个:

{"zoom":13,"tilt":0,"mapTypeId":"hybrid","center":{"lat":45.38591280296377,"lng":19.93632316979983},"overlays":[{"type":"polygon","title":"Polje 1","content":"Vojvodina","fillColor":"#ffbf1a","fillOpacity":0.3,"strokeColor":"#000","strokeOpacity":0.8,"strokeWeight":3,"paths":[[{"lat":"45.37867863632308","lng":"19.948768615722656"},{"lat":"45.370719925928746","lng":"19.941558837890625"},{"lat":"45.36227764550136","lng":"19.92816925048828"},{"lat":"45.359262240003495","lng":"19.942245483398438"},{"lat":"45.35588479505299","lng":"19.955806732177734"},{"lat":"45.35974471568275","lng":"19.958553314208984"},{"lat":"45.36312193024184","lng":"19.959583282470703"},{"lat":"45.365534102931655","lng":"19.960613250732422"},{"lat":"45.36529289029106","lng":"19.96490478515625"},{"lat":"45.37084052080666","lng":"19.970226287841797"}]]}]}

我怎么能这样做?

0 个答案:

没有答案