1

I'm facing challenge with ajax response in my code:

//  ********* PHP code (LoadData.php) ****************
<?php 
include 'Connexion.php';

$requete="SELECT id, pkuid, province, nom, prenom, tel, nom_mammifere, qte_male, qte_femell, ST_AsGeoJSON(geom, 5) as geom FROM mammiferes ORDER BY nom";

$Connection = Connexion::getInstance()->getDB();
$res = $Connection->prepare($requete);
$res->execute();

$features=[];
while($row = $res->fetch(PDO::FETCH_ASSOC)){
    $feature =["type"=> "Feature"];
    $feature["geometry"] = json_decode($row["geom"]);

    unset($row["geom"]);

    $feature["properties"]=$row;
    $features[]=$feature;
    //array_push($features, $feature);
 }

$featureCollection = ["type"=>"FeatureCollection", "features"=>$features];              
echo json_encode($featureCollection);
?>

This works fine and retrieve data using such url: localhost/myproject/loadData.php Now I want to send the output of this file in my javascript file using ajax. Below is my ajax call:

$.ajax({
url:'loadData.php',
type:'POST',    
success:function(response){ 
    alert(response);
},
error:function(xhr, status, error){
        console.log(xhr);
        console.log(status);
        console.log(error);
    }
 });

this retrieve just a part of the php result (for example 75 of 3500 rows) of this result is sometime not a valid json (jsonlint.com). I don't know what is going wrong. Merci

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.