使用AJAX将JSON数据从php文件传递到javascript文件。

时间:2022-12-20 15:25:39

Trying to grab a json variable from a php file and pass to a javascript file. I'm making a leaflet map and I store the information for markers in a mysql database, I then use a php file to pull that information and put it into an array.

尝试从php文件获取json变量并传递给javascript文件。我正在制作一个传单映射,我将标记信息存储在mysql数据库中,然后使用一个php文件来提取这些信息并将其放入一个数组中。

The problem: For some reason the Javascript file is not using the data correctly and I can't figure out why.

问题:出于某种原因,Javascript文件没有正确地使用数据,我也不知道为什么。

get-data.php

get-data.php

$db = new mysqli('localhost', 'testuser', 'testpassword', 'testdb');
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}


$queryStmt = 'SELECT * FROM test';

$geojson = array(
            'type' =>   'FeatureCollection',
            'features' => array()
);

if($result = $db->query($queryStmt)) {
while($row = mysqli_fetch_assoc($result)){
        extract($row);
        $markers = array(
            'type' => 'Feature',
            'properties'        => array (
                'name'          => $name,
                'type'          => $type,
                'id'            => $id,
                'country'       => $country,
                'territory'     => $territory,
                ),
                'geometry'      => array(
                'type'          => 'Point',
                'coordinates'   => array($long,$lat)
                )
            );  

        array_push($geojson['features'], $markers);
}
};

header('Content-type: application/json');
echo json_encode($geojson, JSON_NUMERIC_CHECK);
$db = NULL; 

data.js

data.js

var result = [];
$.get("get-data.php", function(geojson) {
    result = geojson;
}); 

capitolsLayer = L.geoJson(result, {
//lots of stuff with maps and everything

index.php (where all the map code actually goes)

索引。php(所有的地图代码都在这里)

<script type="text/javascript" source="data.js"></script>
<script>
//lots of map code
var map = L.map();
capitolsLayer.addTo(map);
</script>

In the ajax call of data.js I tried adding

在数据的ajax调用中。js我尝试添加

alert(result);

And what popped up was just [object Object]

弹出的是[object对象]

On index.php no errors are shown the markers just simply don't appear.

在索引。php没有错误显示,标记只是没有出现。

EDIT Update with solution! After a lot of reading and learning and LOTS of digging I found the answer! I changed this:

编辑更新解决方案!经过大量的阅读、学习和挖掘,我找到了答案!我改变了:

$.get("get-data.php", function(data) {
    result = data;
}); 
capitols = new L.geoJson(result, {
//map stuff here

To this:

:

$.getJSON("get-data.php", function(data) {
    capitols.addData(data);
}).complete;

capitols = new L.geoJson(null, {
//map stuff here

1 个解决方案

#1


1  

Try this way parsing the JSON data and accessing at json object..

尝试用这种方式解析JSON数据并访问JSON对象。

var result = [];
$.get("get-data.php", function(geojson) {
    result = JSON.parse(geoJson);
    alert (result.properties.name);
});

#1


1  

Try this way parsing the JSON data and accessing at json object..

尝试用这种方式解析JSON数据并访问JSON对象。

var result = [];
$.get("get-data.php", function(geojson) {
    result = JSON.parse(geoJson);
    alert (result.properties.name);
});