2016-10-04 19 views
1

、私はcodeigniterのajaxリクエストでデータベースから値を取得しようとしています...しかし、jsonオブジェクトはconsole.logを置くとnull([])を返します。 !!助けのためのCodeigniter null値ajaxリクエスト

JAVASCRIPT

function list_president() { 
     var section = "1"; 
     $.post(baseurl + 'votos/load_politic', section, 
      function(data) { 
       console.log(data); 
      }); 
    } 

CONTROLLER

public function load_politic() 
{ 
if ($this->input->is_ajax_request()) { 

     $section = $this->input->post('section'); 
    $result = $this->politic->get_president($section); 

    echo json_encode($result); 
    } 
} 

MODEL

public function get_president($section){ 


    $this->db->select("p.POLITIC_NAME, p.POLITIC_LASTNAME, p.POLITIC_SIDE, p.POLITIC_CHARGE"); 
    $this->db->from("politics p"); 
    $this->db->where("SECTION_ID",$section); 

    $result= $this->db->get(); 

    return $result->result(); 
} 

感謝!!

+0

あなたがge idでこれを調べると、1行をフェッチしているので、$ result-> row()を返す必要があります。この後、あなたの$ .postコールでconsole.log(data)を呼び出して、あなたに何を得るか教えてください。 – Franco

+0

@Franco私は$ result-> result()を変更しました。 ... $ result-> row()...とnullを返す=( –

+0

@charlietflが正しい方向を指していますが、これは私が常にこの種のものにajax calを使用しているため、私の注意を喚起しています。彼の提案があなたの問題を解決したことを願っています。 – Franco

答えて

1

サーバーにキーと値のペアを送信していません。単なる値です。

ので$this->input->post('section');

あなたはまた、$resultかのリターンを送っているものを確認するか、チェックされていない

var section = {section: "1"}; 

var section = "1"; 

を変更してみてください何であるか基本的に何$_POST['section']はありません何でも

+0

TY !!! –

関連する問題