繁体   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