2017-01-03 4 views
0

Es ist meine erste Zeit-Codierung mit JS & JSON & Ich habe eine Fehlermeldung, wenn ich getJSON verwendet:Parse Fehler JSON.parse bei der Verwendung von getJSON

parsererror

SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data

return window.JSON.parse(data);

Hier mein Code:

$.getJSON("../processeur.php",{ 
    idProg: idp, 
    exercice: exo, 
    ajax: "true"}) 
    .done(
    function(response) 
    { 
    // alert("success"); 

     var options =""; 
     if(response != null) 
     { 
      var length = response.data.length; 
      for(var i=0; i<length; i++) 
      { 
       options +="<option value = '"+response.data[i]+"'>"+response.data[i];"</option>"; 
      } 
     } 


     $("#Liberation tbody").append 
     (
      "<tr>"+    
       "<td align='center'><input class='liberationL' name='liberationL' type='text'/></td>"+ 
       "<td align='center'><input class='serviceL' name='serviceL' type='text'/></td>"+ 
       //liste déroulante des codes projets destinataires 
       "<td align='center'>"+"<select class='codest' name ='codest' id=listecodes >"+"<option>Aucun</option>"+options+"</select>"+"</td>"+ 
//    "<td align='center'><input class='dateL' name='dateL' type='text'/><span><br>jj-mm-AAAA</span></td>"+ 
     "<td align='center'><input type='text' class='dateL' id='DateF' name='dateL' onclick='javascript:onCalendar_click();'/></td>"+ 
       "<td align='center'><input class='montantL' name='montantL' type='text'/></td>"+   
       //liste déroulante des types de mouvements    
       "<td align='center'>"+"<select class='mouvementL' name='mouvementL'>"+"<option value='lc'>LC(-)</option>"+"<option value='vc'>VC(+)</option>"+"<option value='ci'>CI</option>"+"</select>"+"</td>"+ 
       "<td align='center'>"+ 
       "<img src='../images/enregistrer.png' class='btnEnregistrerLiberation'"+"style='cursor: pointer;'/>"+' '+"<img src='../images/supprimer.png' class='btnSuppLiberation'"+"style='cursor: pointer;'/>"+"</td>"+ 
      "</tr>"); 
      $(".btnEnregistrerLiberation").bind("click",EnregistrerLiberation); 
      $(".btnSuppLiberation").bind("click",SupprimerLib); 

    }) 
    .fail(function(jqxhr, textStatus, err){ 
     alert("error : " + textStatus); 
     console.log(textStatus, err); 
    }); 

Hier mein PHP-Code:

include './BD/T_mouvements.php'; 
include '../sql.php'; 
require './jsonwrapper/jsonwrapper.php'; 

$idProg = $_GET['idProg']; 
$exercice = $_GET['exercice']; 

$array = array(); 
$liste = selectionnerListePro($exercice, $idProg); 
//$liste = selectionnerListePro(2011, 4); 

foreach ($liste as $item) 
{  
    $array[] = array($item); 
} 
echo "{\"data\":". json_encode($array) . "}"; 
exit(); 

Und hier ist das Ergebnis meiner PHP-Code, wenn die Parameter ar e wählen manuell für die Funktion, die Abfrage ausgeführt wird (z.Bsp 2011 & 4):

{"data":[["DEV-SID"],["ENTREPOTDUI"],["HYDROGEOL"],["MES-TEMPS"],["MET-ENTREPO"],["MIG-BO\/XI"],["SID-AMODG"],["SID-ARCHID"],["SID-DSI"],["SID-FNGE"],["SID-OT-POL"],["SID-PILOTAG"],["SID-USAGRH"],["SIG-3D"],["SIG-ALTERNA"],["SIG-BDTOPO"],["SIG-CAO-DAO"],["SIG-DON-PDI"],["SIG-DONNEES"],["SIG-ORTHO"],["SIG-PLATGEO"],["SIG-STRUCTU"],["SIG-TOURNEE"],["SIG-WEB-PDI"],["STAT-CREDOC"]]} 

Ich verstehe nicht, wo mein Fehler ist ..

+2

http://json.parser.online.fr/ reduziert werden - das JSON ist nicht gültig - auch Ihre Paste hat ein verstecktes Zeichen in Vorderseite der Daten - Das gültige Objekt könnte wie folgt aussehen: {"data": ["DEV-SID", "ENTREPOTDUI", "HYDROGEOL", "MES-TEMPS", "MET-ENTREPO", "MIG-BO \ /" XI "," SID-AMODG "," SID-ARCHID "," SID-DSI "," SID-FNGE "," SID-OT-POL "," SID-PILOTAG "," SID-USAGRH "," SIG- 3D, SIG-ALTERNA, SIG-BDTOPO, SIG-CAO-DAO, SIG-DON-PDI, SIG-DONNEES, SIG-ORTHO, SIG-PLATGEO, SIG-STRUCTU "," SIG-TOURNEE "," SIG-W EB-PDI "," STAT-CREDOC "] }' – mplungjan

+0

Warum? Wo habe ich einen Fehler gemacht? – LynxWeb

+2

Sie können 'json_encode' für das gesamte Ergebnis verwenden. Etwas wie 'echo json_encode ([' data '=> $ array]); ' –

Antwort

0

Ich habe versucht, mit AJAX, dies zu tun, & es endlich funktioniert !! Hier mein Code:

$.ajax(
    { 
     type: "GET", 
    url: "../processeur.php", 
    dataType: "json", 
     data: dataString, 
    success: function(response) 
     { 
      //alert("success"); 

      var options =""; 

      if(response != null) 
      { 

       for(var i=0; i<response.data.length; i++) 
       { 
        options +="<option value = '"+response.data[i]+"'>"+response.data[i];"</option>"; 
       } 
      } 


      $("#Liberation tbody").append 
      (
       "<tr>"+    
        "<td align='center'><input class='liberationL' name='liberationL' type='text'/></td>"+ 
        "<td align='center'><input class='serviceL' name='serviceL' type='text'/></td>"+ 
        //liste déroulante des codes projets destinataires 
        "<td align='center'>"+"<select class='codest' name ='codest' id=listecodes >"+"<option>Aucun</option>"+options+"</select>"+"</td>"+ 
    //    "<td align='center'><input class='dateL' name='dateL' type='text'/><span><br>jj-mm-AAAA</span></td>"+ 
      "<td align='center'><input type='text' class='dateL' id='DateF' name='dateL' onclick='javascript:onCalendar_click();'/></td>"+ 
        "<td align='center'><input class='montantL' name='montantL' type='text'/></td>"+   
        //liste déroulante des types de mouvements    
        "<td align='center'>"+"<select class='mouvementL' name='mouvementL'>"+"<option value='lc'>LC(-)</option>"+"<option value='vc'>VC(+)</option>"+"<option value='ci'>CI</option>"+"</select>"+"</td>"+ 
        "<td align='center'>"+ 
        "<img src='../images/enregistrer.png' class='btnEnregistrerLiberation'"+"style='cursor: pointer;'/>"+' '+"<img src='../images/supprimer.png' class='btnSuppLiberation'"+"style='cursor: pointer;'/>"+"</td>"+ 
       "</tr>"); 
       $(".btnEnregistrerLiberation").bind("click",EnregistrerLiberation); 
       $(".btnSuppLiberation").bind("click",SupprimerLib); 
     }, 
     error: function(jqxhr, textStatus, err){ 
     alert("error : " + textStatus); 
     console.log(textStatus, err); 
     } 
    }); 
1

Die json_encode() Funktion wird alles für Sie tun. Erstellen Sie einfach die gewünschte Datenstruktur als Array oder Objekt, und lassen Sie json_encode() die ganze Arbeit erledigen.

So echo "{\"data\":". json_encode($array) . "}"; ändern

echo json_encode(array('data'=>$array)); 

Ich bin auch ziemlich sicher, dass Sie einige unnötige Code aus Ihrem Skript entfernen können. Sie scheinen ein Array von Arrays von selectionnerListePro() zurück zu bekommen und dieses Array von Arrays dann in ein anderes Array von Arrays zu verarbeiten, ohne dabei Änderungen vorzunehmen.

Also die

$liste = selectionnerListePro($exercice, $idProg); 
//$liste = selectionnerListePro(2011, 4); 

foreach ($liste as $item) 
{  
    $array[] = array($item); 
} 
echo json_encode(array('data'=>$array)); 

kann zu

$liste = selectionnerListePro($exercice, $idProg); 
echo json_encode(array('data'=>$liste)); 
+0

Dank das Ergebnis ist mehr "sauber": {"Daten": ["DEV-SID", "ENTREPOTDUI HYDROGEOL, MES-TEMPS, MET-ENTREPO, MIG-BO/XI, SID-AMODG, SID-ARCHID, SID-DSI, SID-FNGE "SID-OT-POL", "SID-PILOTAG", "SID-USAGRH", "SIG-3D", "SIG-ALTERNA", "SIG-BDTOPO", "SIG-CAO-DAO", "SIG- DON-PDI, SIG-DONNEES, SIG-ORTHO, SIG-PLATGEO, SIG-STRUCTU, SIG-TOURNEE, SIG-WEB-PDI, STAT-CREDOC Aber ich habe immer noch einen Fehler mit getJSON: s – LynxWeb

+0

Welcher Fehler und in welcher Zeile? – RiggsFolly

+0

Ich habe diese Eingabe validiert und habe immer noch ungültige Zeichen ... – Marco

Verwandte Themen