我有像CodeIgniter这样的自定义框架。 我使用Ajax从数据库中获取数据,但有以下消息:“数组到字符串转换” 。
是什么原因以及如何解决此回声?
控制器
public function getallserials()
{
if(isset($_POST['action'],$_POST['notebook_id'],$_POST['resseler_id'],$_POST['pos_id'])&&$_POST['action']=="getserial")
{
$allNoteBookSerials=$this->loadModel("NotebookRessellerDetails")->retrieveAllData("where `deleted`=? and `notebook_id`=? and `id_resseller`=? ",array(1,$_POST['notebook_id'],$_POST['resseler_id']));
$addedSerialToPos=$this->loadModel("PosNotebookSerialsModel")->retrieveAllData("where `deleted`=? and `notebook_id`=? and `resseler_id`=? ",array(1,$_POST['notebook_id'],$_POST['resseler_id']));
if(is_array($addedSerialToPos))
{
// get serials only
foreach ($addedSerialToPos as $key => $value):
$RegisterdSerials[$key] =$value['serial'];
endforeach;
}
if(is_array($allNoteBookSerials))
{
if(is_array($allNoteBookSerials)&&is_array($RegisterdSerials))
{
//$x=array_values(array_diff($RegisterdSerials,$allNoteBookSerials)) ;
$x=array_diff($RegisterdSerials,$allNoteBookSerials) ;
$response['status']="success";
$response['x']=(isset($x))?$x:null;
echo $this->loadAjax()->DataToJson($response);
exit();
}
}
}
}
我的Ajax代码
$("body").on("change","#notebook_id",function () {
var notebook_id=$(this).val()
var pos_id=$("#pos_id").val();
var resseler_id=$("#resseler_id").val();
var action="getserial";
// start ajax
$.ajax({
url: "<?php echo BASE_URL.'PosNoteBookserials/getallserials' ?>",
method:"post",
data:{notebook_id:notebook_id,pos_id:pos_id,resseler_id:resseler_id,action:action},
beforeSend:function(){
},
statusCode: {
404: function() {
alert( "page not found" );
},
},
success:function (data) {
/*
var text='';
var result = jQuery.parseJSON(data);
if(result['status']=="success")
{
for (i=0;i<result['allNoteBookSerials'].length;i++)
{
text+='<label class="label label-success">'+result['allNoteBookSerials'][i]['serial']+'</label>';
$("#res").html(text);
}
}else{
}
*/
/*
*/
alert(data);
var text='';
var result = JSON.parse(data);
if(result['status']=="success")
{
for (i=0;i<result['x'].length;i++)
{
text+='<label class=" label label-success">'+result['x'][i]+'</label>';
$("#res").html(text);
}
}else{
}
}
})
当我发送请求时,我得到x的响应,并以正确的数据作为数组,但是错误“数组到字符串的转换” 出现在包含此代码的行上。
答案 0 :(得分:0)
public function getallserials()
{
if(isset($_POST['action'],$_POST['notebook_id'],$_POST['resseler_id'],$_POST['pos_id'])&&$_POST['action']=="getserial")
{
$allNoteBookSerials=$this->loadModel("NotebookRessellerDetails")->retrieveAllData("where `deleted`=? and `notebook_id`=? and `id_resseller`=? ",array(1,$_POST['notebook_id'],$_POST['resseler_id']));
$addedSerialToPos=$this->loadModel("PosNotebookSerialsModel")->retrieveAllData("where `deleted`=? and `notebook_id`=? and `resseler_id`=? ",array(1,$_POST['notebook_id'],$_POST['resseler_id']));
$RegisterdSerials=array();
if(is_array($addedSerialToPos))
{
// get serials only
foreach ($addedSerialToPos as $key => $value):
$RegisterdSerials[$key] =$value['serial'];
endforeach;
}
if(is_array($allNoteBookSerials))
{
if(is_array($allNoteBookSerials) && is_array($RegisterdSerials))
{
//$x=array_values(array_diff($RegisterdSerials,$allNoteBookSerials)) ;
$x=array_diff($RegisterdSerials,$allNoteBookSerials) ;
$response['status']="success";
$response['x']=(is_array($x))?$x:[];
echo json_encode($response);
exit();
}
}
}
}