0

次の問題があります。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>
4

2 に答える 2

0

あなたのコントローラーで:

class Json extends CI_Controller {

function __construct() 
{
    parent::__construct();
    $this->load->model('functionsonproducts'); 
}
function index()
{
    $functionsOnProducts = $this->functionsonproducts->getProducts();
    echo json_encode($functionsOnProducts);
}
}

あなたの見解では

function getAllFunctionsOnProducts() {
    $.ajax({type : "GET",
        url : "http://localhost/tddd27/index.php/json/index",
        data : { },
        success : function(result){
            var functionsOnProducts= jQuery.parseJSON(result);
            console.log(funtionsOnProducts);
            /* If you want to go through every product:
               jQuery.each(functionsOnProducts,function(){
            })
            */
}
于 2013-04-14T14:09:23.167 に答える
0

タグがコードのどこから来ているのかわかりませんが、リターンを入れてください。json_endcode() ステートメントの後、他のものがエコーされないようにします

于 2013-04-13T09:38:37.353 に答える