I get an "Uncaught SyntaxError: Unexpected string " when I try to parse my xhr.responseText, I've looked for answers but cannot find any that can relate to my problem directly. I seem to understand that my xhr.responseText may be JSONP because it is manipulated byt the callback but that doesn't help me, how can I just have a JSON? I want to be able to use my JSON in my Javascript with the Google Maps API. I'm really confused and just trying to make this work, thanks for reading and possibly helping.

当我尝试解析我的xhr.responseText时,我得到一个“Uncaught SyntaxError:Unexpected string”,我找到了答案,但找不到任何与我的问题直接相关的问题。我似乎明白我的xhr.responseText可能是JSONP,因为它是由回调操纵的,但这对我没有帮助,我怎么才能拥有JSON?我希望能够在我的Javascript中使用我的JSON和Google Maps API。我真的很困惑,只是想让这项工作,感谢阅读和可能的帮助。

Here's my PHP script.

这是我的PHP脚本。

<?php

header("Content-type: application/json; charset=utf-8");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");

require("include/param_bd.inc");
try {
    $connBD = new PDO("mysql:host=$dbHote; dbname=$dbNom", $dbUtilisateur, $dbMotPasse, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $connBD->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    exit("Erreur lors de la connexion à la BD :<br />\n" . $e->getMessage());
}
try {
    $reqZaps = "SELECT * FROM leszaps";
    $prepReqZaps = $connBD->prepare($reqZaps);
    $prepReqZaps->execute();
    $prepReqZaps->setFetchMode(PDO::FETCH_OBJ);
} catch (PDOException $e) {
    exit("Erreur lors de l'exécution de la requête SQL :<br />\n" . $e->getMessage() . "<br />\nREQUÊTE = " . $reqZaps);
}
if (!( $infoZaps = $prepReqZaps->fetch() ))
    $msgErreur = "Aucun Zap trouvé";
else {
    do
    {
        $id = $infoZaps->id;
        $coordinates = $infoZaps->coordinates;
        $arrondissement = $infoZaps->arrondissement;
        $numerocivil = $infoZaps->numerocivil;
        $rue = $infoZaps->rue;
        $nombatiment = $infoZaps->nombatiment;
        echo "{\n";
        echo "\t\"id\": \"$id\",\n";
        echo "\t\"coordinates\": \"$coordinates\",\n";
        echo "\t\"arrondissement\": \"$arrondissement\",\n";
        echo "\t\"numerocivil\": \"$numerocivil\",\n";
        echo "\t\"rue\": \"$rue\"\n";
        echo "\t\"nombatiment\": \"$nombatiment\"\n";
        echo "}\n";
    }
    while ($infoZaps = $prepReqZaps->fetch());

}
$prepReqZaps->closeCursor();
$connBD = null;

if ($msgErreur != "undefined" && $msgErreur != null) {
    echo "{\n";
    echo "\t\"erreur\":\n";
    echo "\t{\n";
    echo "\t\t\"message\": \"" . str_replace("\"", "\\\"", $msgErreur) . "\"\n";
    echo "\t}\n";
    echo "}\n";
}
?>

And here's my Javascript script

这是我的Javascript脚本

var xhr;
var donneesChargees = false;
var objReperes;
var reperes;

chargerKml();

function chargerKml() {

    var lienDocChargement = './script_load_kml_get.php';

    var erreur = false;

    chargerScriptAsync('./script_load_kml_bd.php', function(){
        console.log("Les ZAP ont été chargées dans la base de données");
    });
    if (window.XMLHttpRequest) {

        httpRequest = new XMLHttpRequest();

    } else if (window.ActiveXObject) {
        httpRequest = new
                ActiveXObject("Microsoft.XMLHTTP"); }
    try {
        xhr = new XMLHttpRequest();
    } catch (e) {
        alert('Erreur: Impossible de créer l\'objet XMLHttpRequest');
        erreur = true;
    }
    if (!erreur)
    {
        xhr.onreadystatechange = xhrCallback;
        xhr.open('GET', lienDocChargement, true);
        xhr.send(null);
    }
}

function xhrCallback()
{

    if (xhr.readyState == 4)
    {
        if (xhr.status != 200)
            alert('Erreur: La requête HTTP a échoué (code=' + xhr.status + ')');
        else
        {
                            objReperes = JSON.parse( xhr.responseText );
            creerReperes();
        }
    }
}

function creerReperes()
{

    try { 

        var strReperes;

                //$arrondissement = objReperes.$arrondissement;
        strReperes = '[' + xhr.responseText + ']';
        strReperes = strReperes.replace(/[\n\r\t]/g, '');
        strReperes = strReperes.replace(/}{/g, '}~{');
        strReperes = strReperes.split('~');     
        var coordonnees;
                var id;
        reperes = '[';
        for (var i = 0; i < strReperes.length; i++){
                         var numberPattern = /\d+/g;
                        id = strReperes[i].split(',')[0];
                        id = id.match( numberPattern );
            coordonnees = strReperes[i].split(',')[1] + ',' + strReperes[i].split(',')[2];
            coordonnees = coordonnees.replace(/"/g, '');
            coordonnees = coordonnees.replace('coordinates: ', '');
            coordonnees = coordonnees.split(',');
            reperes += '{"long": ' + coordonnees[0] + ', "lat": ' + coordonnees[1] + ', "id": ' + id + '}';
            if (i != strReperes.length - 1) {
                reperes += ', ';
            }
        }
        reperes += ']';
        reperes = JSON.parse(reperes);


    } catch (e) {
        alert('ERREUR: La réponse AJAX n\'est pas une expression JSON valide.');
        return;
    }


}

As you can see in creeReperes() i need to do some unorthodox manipulation with the xrhresponsetext instead of using a JSON.

正如你在creeReperes()中看到的,我需要使用xrhresponsetext而不是使用JSON进行一些非正统的操作。

Oh and sorry if some of the code is in french.

哦,对不起,如果一些代码是法语。

1 个解决方案

#1


0

if (!( $infoZaps = $prepReqZaps->fetch() ))
    $msgErreur = "Aucun Zap trouvé";
else {

    $rows = array();
    while ($infoZaps = $prepReqZaps->fetch()) {
        $rows[] = $infoZaps;
    }
    echo json_encode($rows);
}

$prepReqZaps->closeCursor();
$connBD = null;

更多相关文章

  1. python解压zip脚本
  2. 使用python脚本配置zabbix发送报警邮件
  3. 见证历史!Python或将取代VBA,成为Excel官方脚本语言!
  4. 解决Linux下运行Python脚本显示“: 没有那个文件或目录”的问题
  5. 通过Excel / VBA运行Python脚本
  6. 利用Dnspod api批量更新添加DNS解析【python脚本】
  7. 是否可以在没有安装铁python的PC上运行编译的铁python脚本?
  8. python脚本下载并解码MNIST数据遇到的问题
  9. 从另一个Python脚本调用Python脚本的最佳方式是什么?

随机推荐

  1. 把android平板USB上输出LOG方法
  2. android和js之间的简单交互
  3. android统计图绘制
  4. Android(安卓)listview下拉刷新 上拉加载
  5. Android按键消息传播流程
  6. Android触控
  7. Android 发送短信 源代码
  8. android input 事件传递主要流程
  9. android点滴4
  10. Android studio 中调用ndk-build 进行编