Javascript 使用getJSON时解析错误JSON.Parse
这是我第一次使用JS&JSON编码&当我使用getJSON时,我收到一条错误消息: 解析器错误 SyntaxError:JSON.parse:JSON数据第2行第1列处的意外字符 返回window.JSON.parse(数据) 这是我的代码:Javascript 使用getJSON时解析错误JSON.Parse,javascript,php,arrays,json,Javascript,Php,Arrays,Json,这是我第一次使用JS&JSON编码&当我使用getJSON时,我收到一条错误消息: 解析器错误 SyntaxError:JSON.parse:JSON数据第2行第1列处的意外字符 返回window.JSON.parse(数据) 这是我的代码: $.getJSON("../processeur.php",{ idProg: idp, exercice: exo, ajax: "true"}) .done( function(response) {
$.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代码在为运行查询的函数手动选择参数时的结果(例如2011和4):
我不明白我的bug在哪里。我试着用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 );
}
});
$.ajax(
{
键入:“获取”,
url:“../processeur.php”,
数据类型:“json”,
数据:dataString,
成功:功能(响应)
{
//警惕(“成功”);
var期权=”;
if(响应!=null)
{
对于(var i=0;i函数,json_encode()
将为您完成所有工作。只需创建您想要的数据结构作为数组或对象,然后离开json_encode()
即可完成所有艰巨的工作
所以将echo“{\“data\:”.json\u encode($array)。“}”
更改为
echo json_encode( array('data'=>$array) );
我还非常确定您可以从脚本中删除一些不必要的代码。您似乎从selectionNerListPro()
中获取了一个数组,然后将该数组处理为另一个数组,但在此过程中不做任何修改
那么这个
$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) );
-JSON无效-您的粘贴在数据前面有一个隐藏字符-有效对象可能看起来像{“data”:[“DEV-SID”、“enterpotdui”、“hydrogoo”、“MES-TEMPS”、“MET-enterpo”、“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_编码
。类似于echo json_编码(['data'=>$array]))
我不明白我的JSON为什么无效。我遵循API,尝试对整个结果进行编码,但似乎什么也没有改变:sDo您是否将PHP源代码保存为带有BOM的UTF-8?谢谢,结果更“干净”:{“数据”:[“DEV-SID”、“enterpotdui”、“hydrogool”、“MES-TEMPS”、“MET-enterpo”、“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-DONEES、SIG-ORTHO、SIG-PLATGEO、SIG-STRUCTU、SIG-TOURNEE、SIG-WEB-PDI、STAT-CREDOC”]}但我仍然在getJSON中遇到一个错误:我不知道在哪里。“clean:
部分来自于,因为它不是我建议的代码所产生的。当我说clean时,我的意思是我的结果是好的{“data”:[“DEV-SID”,“enterpotdui”,“hydrogool”,“MES-TEMPS”,“ME”T-Enterpo,“米格-BO”\/十一、SID-AMODG、SID-ARCHD,“SID-DSI”SID-FNGE”、“SID-OT-PO”L、SID-PILOTAG、SID-USAGRH,“SIG-3D”SIG-ALTERNA”、“SIG-BD拓扑学,"SIG-CAO-DAO",,“SIG-DON-PDI”、“SIG-D”ONNEES,“SIG-ORTHO”,“SIG-PLATGEO”、“SIG-S”TRUCTU”、“SIG-TOURNEE","SIG-WEB-PDI“STA”T-CREDOC“]}对于我的getJSON,错误消息是:SyntaxError:JSON.parse:JSON数据返回窗口.JSON.parse(data)第2行第1列的意外字符;我放弃了getJSON并使用ajax尝试。
$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) );