次の問題があります。Codeigniter Controller を使用して Json 出力を生成します。私のコントローラーは:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Json extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model ('functionsonproducts');
}
function index(){
$data ['rows'] = $this->functionsonproducts->getProducts ();
echo json_encode($data ['rows']);
}
}
次の結果を取得したい
[{"id":"1","name":"Milk","description":"200gr","price":"10"},{"id":"3","name":"","description":"","price":"0"}]
しかし、コントローラーの関数をjqueryで呼び出すと、
Jクエリ
$.get("http://localhost/tddd27/index.php/json/index",
function(data){
console.log(data);
});
最後に不明な < /html> タグを取得するため、jquery の json パーサーを使用しようとすると、この不要なタグが原因でエラーが発生します。
コンソール出力:
[{"id":"1","name":"Milk","description":"200gr","price":"10"},{"id":"3","name":"","description":"","price":"0"}]</html>