简体   繁体   English

解析错误JSON。使用getJSON时解析

[英]Parse Error JSON.Parse when using getJSON

It's my 1st time coding with JS & JSON & I've a error message when I used getJSON : 这是我第一次使用JS和JSON进行编码,使用getJSON时出现错误消息:

parsererror 分析错误

SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data SyntaxError:JSON.parse:JSON数据的第2行第1列出现意外字符

return window.JSON.parse( data ); 返回window.JSON.parse(data);

Here my 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 );
    });

Here my php code : 这是我的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();

And here the result of my php code when parameters are choose manually for function which runs query (for exemple 2011 & 4) : 这是我的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"]]}

I don't understand where is my bug.. 我不知道我的虫子在哪里。

The json_encode() function will do everything for you. json_encode()函数将为您做所有事情。 Just create the data structure you want as an array or an object and the leave json_encode() to do all the hard work. 只需将所需的数据结构创建为数组或对象,然后保留json_encode()即可完成所有艰苦的工作。

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

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

I am also fairly sure you can remove some unnecessary code from your script. 我也相当确定您可以从脚本中删除一些不必要的代码。 You seem to be getting an array of arrays back from selectionnerListePro() and then processing that array of arrays into another array of arrays, but making no modifications in the process. 您似乎正在从selectionnerListePro()取回一个数组数组,然后将该数组数组处理为另一个数组数组,但是在此过程中未进行任何修改。

So this 所以这

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

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

Could be reduced to 可以减少到

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

I tried to do this with AJAX, & it finally works !! 我试图用AJAX来做到这一点,并且终于可以了! Here my 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 );
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM