簡體   English   中英

解析錯誤JSON。使用getJSON時解析

[英]Parse Error JSON.Parse when using getJSON

這是我第一次使用JS和JSON進行編碼,使用getJSON時出現錯誤消息:

分析錯誤

SyntaxError:JSON.parse:JSON數據的第2行第1列出現意外字符

返回window.JSON.parse(data);

這是我的代碼:

$.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 );
    });

這是我的PHP代碼:

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();

這是我的php代碼在為運行查詢的函數手動選擇參數時的結果(例如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"]]}

我不知道我的蟲子在哪里。

json_encode()函數將為您做所有事情。 只需將所需的數據結構創建為數組或對象,然后保留json_encode()即可完成所有艱苦的工作。

因此,更改echo "{\\"data\\":". json_encode($array) . "}"; echo "{\\"data\\":". json_encode($array) . "}";

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

我也相當確定您可以從腳本中刪除一些不必要的代碼。 您似乎正在從selectionnerListePro()取回一個數組數組,然后將該數組數組處理為另一個數組數組,但是在此過程中未進行任何修改。

所以這

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

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

可以減少到

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

我試圖用AJAX來做到這一點,並且終於可以了! 這是我的代碼:

$.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 );
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM