Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter中尝试使用getJSON时遇到问题_Php_Javascript_Ajax_Codeigniter - Fatal编程技术网

Php 在codeigniter中尝试使用getJSON时遇到问题

Php 在codeigniter中尝试使用getJSON时遇到问题,php,javascript,ajax,codeigniter,Php,Javascript,Ajax,Codeigniter,我正在尝试使用codeigniter将AJAX调用方法实现到我的网站中,这就是为什么我想使用AJAX以便从数据库中获得实时更新的原因 单击按钮可以工作并显示所有JSON数据,但问题是当我尝试显示一个特定数组时,它无法识别它,它似乎在遍历每个字符 还有一件事是,当调用警报时,我可以看到html和head标记 我希望能够打印特定阵列,例如阵列id“ 我做了一些事情来尝试解决它 我已经通知了数据类型,发现它是一个字符串 我已经设法让ajax在普通PHP中工作,而不是 codeigniter,所以他们

我正在尝试使用codeigniter将AJAX调用方法实现到我的网站中,这就是为什么我想使用AJAX以便从数据库中获得实时更新的原因

单击按钮可以工作并显示所有JSON数据,但问题是当我尝试显示一个特定数组时,它无法识别它,它似乎在遍历每个字符

还有一件事是,当调用警报时,我可以看到html和head标记 我希望能够打印特定阵列,例如阵列id“

我做了一些事情来尝试解决它

  • 我已经通知了数据类型,发现它是一个字符串

  • 我已经设法让ajax在普通PHP中工作,而不是 codeigniter,所以他们的数据库没有问题

  • 我还尝试使用jQuery.parseJSON,因此我可以强制使用它 但是脚本没有运行

  • 我使用了JSONlint,它显然是一个有效的JSON

任何帮助都将不胜感激

控制器

public function insertJSON()
{
    $this->load->model("values");
    $queryresults = $this->values->getDb();

    $arrays = array($queryresults);

    echo json_encode($arrays);  
}
看法


$data[]=$arr;//@Ben是对的,
$arr
从哪里来

public function insert()
{

    $this->load->model("values");

    $queryresults = $this->values->Query();

    $array = array();

    foreach($queryresults as $row)
    {
        $array[] =  $row->postcode;
    }

    echo json_encode($array);
    exit; 
//    Just echo it out, no need to load view!
//    $this->load->view('answer', $data);
}

缺少的变量$arr应该是循环之前声明的$array变量。这将执行以下操作:

public function insert()
{

    $this->load->model("values");

    $queryresults = $this->values->Query();

    $array = array();

    foreach($queryresults as $row)
    {
        $array[] =  $row->postcode;
    }

    $data = array();

    $data[] = $array; // <--- The fix!

    echo json_encode($data);

    $this->load->view('answer', $data);
}
公共函数插入()
{
$this->load->model(“值”);
$queryresults=$this->values->Query();
$array=array();
foreach($queryresults作为$row)
{
$array[]=$row->postcode;
}
$data=array();
$data[]=$array;//加载->查看('answer',$data);
}
在进行实际检查之前,请考虑对代码进行双重检查

$data[] = $arr; // <--- This $arr doesn't exist.

echo json_encode($data);
public function insert()
{

    $this->load->model("values");

    $queryresults = $this->values->Query();

    $array = array();

    foreach($queryresults as $row)
    {
        $array[] =  $row->postcode;
    }

    echo json_encode($array);
    exit; 
//    Just echo it out, no need to load view!
//    $this->load->view('answer', $data);
}
public function insert()
{

    $this->load->model("values");

    $queryresults = $this->values->Query();

    $array = array();

    foreach($queryresults as $row)
    {
        $array[] =  $row->postcode;
    }

    $data = array();

    $data[] = $array; // <--- The fix!

    echo json_encode($data);

    $this->load->view('answer', $data);
}