2012-05-03 11 views
0

をやってデバッグするように見えることはできません。私の見解については http://www.jotorres.com/2012/01/using-jquery-and-ajax-with-codeigniter/:私はここにチュートリアル以下のよ間違っ

、私が持っている:

<script type="text/javascript" src="<?php echo jquery ?>"></script> 

<button type="button" name="getdata" id="getdata">Get Data.</button> 

<div id="result_table"> 

</div> 

<script type="text/javascript" language="javascript"> 
$('#getdata').click(function(){ 

    $.ajax({ 
      url: "<?php echo base_url().'users/get_all_users';?>", 
      type:'POST', 
      dataType: 'json', 
      success: function(output_string){ 
        $("#result_table").append(output_string); 
       } // End of success function of ajax form 
      }); // End of ajax call 
    }); 
</script> 

ヘッダーとフッターは、主にウェブサイト上の他のページにリンクされているが、ヘッダと最初の関連セクションは次のとおりです。

<!DOCTYPE html> 
<!-- Website template by Jeremiah Tantongco, [email protected] --> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title><?php echo $title ?> </title> 
     <link rel="stylesheet" href="<?php echo template ?>" type="text/css" /> 
     <link rel="stylesheet" href="<?php echo display_elements ?>" type="text/css" /> 
    </head> 

注:テンプレートとDISP lay_elementsは、設定/定数で宣言されたサーバ上のどこかのCSSファイルを指す変数です。

注:jqueryは、サーバ上の最新のjqueryファイルを指すconfig /定数で宣言された変数です。これは最新の開発者向けバージョンです。コントローラメソッドの

:私はデータの取り込みボタンでページに行くとき

public function test(){ 
    $this->renderTemp_noData('users/test', 'Test page'); 
} 

public function get_all_users(){ 
    $query = $this->db->get('users'); 

    if($query->num_rows > 0){ 
     $header = false; 
     $output_string = ""; 
     $output_string .= "<table border='1'>\n"; 
     foreach ($query->result() as $row){ 
      $output_string .= "<tr>\n"; 
      $output_string .= "<th>{" . $row->uid ."}</th>\n"; 
      $output_string .= "</tr>\n"; 
     } 
     $output_string .= "</table>\n"; 

    } 
    else{ 
     $output_string = "There are no results"; 
    } 
    //echo $output_string; 
    echo json_encode($output_string); 
} 

public function renderTemp_noData($page, $title) { 
    $data['title'] = $title; 
    $accountType = $this->session->userdata('aid'); 

    switch ($accountType) { 
     case 0: 
      $this->load->view('templates/LI_header', $data); 
      $this->load->view($page); 
      $this->load->view('templates/LI_footer'); 
      break; 
     case 1: 
      $this->load->view('templates/LI_header_role1',$data); 
      $this->load->view($page); 
      $this->load->view('templates/LI_footer_role1'); 
      break;   
     case 2: 
      $this->load->view('templates/LI_header_role2',$data); 
      $this->load->view($page); 
      $this->load->view('templates/LI_footer_role2'); 
      break; 
     case 3: 
      $this->load->view('templates/LI_header_role3',$data); 
      $this->load->view($page); 
      $this->load->view('templates/LI_footer_role3'); 
      break; 
     default: 
      redirect('/sessions/log_in/','refresh'); 
    } 
} 

は、私はそれを押すと、何も起こりません。だから誰かが偉大なエラーメッセージを公開する方法を知っている場合。また、私はGET_ALL_USERSのための出力を見るためにテストするとき、これは私が得るものです:

"<table border='1'>\n<tr>\n<th>{1}<\/th>\n<\/tr>\n<tr>\n<th>{2}<\/th>\n<\/tr>\n<tr>\n<th>{3}<\/th>\n<\/tr>\n<tr>\n<th>{4}<\/th>\n<\/tr>\n<tr>\n<th>{5}<\/th>\n<\/tr>\n<tr>\n<th>{6}<\/th>\n<\/tr>\n<tr>\n<th>{7}<\/th>\n<\/tr>\n<tr>\n<th>{8}<\/th>\n<\/tr>\n<tr>\n<th>{9}<\/th>\n<\/tr>\n<tr>\n<th>{10}<\/th>\n<\/tr>\n<tr>\n<th>{12}<\/th>\n<\/tr>\n<tr>\n<th>{13}<\/th>\n<\/tr>\n<tr>\n<th>{14}<\/th>\n<\/tr>\n<tr>\n<th>{15}<\/th>\n<\/tr>\n<tr>\n<th>{16}<\/th>\n<\/tr>\n<tr>\n<th>{17}<\/th>\n<\/tr>\n<tr>\n<th>{18}<\/th>\n<\/tr>\n<\/table>\n" 

JSONはこのように見えることになっていますか? JSON、Jquery、AJAXを初めて使用したのは初めてです。また、数値は正しいです。私のデータベースを見ると、無11

+1

あなたはFirebug、Web Developerのようなブラウザアドオンで応答をチェックする必要があります。私はクロムが内蔵されていると思います。どのブラウザを使用していますか?plzはIEと言っていません –

+0

現場でFirefox、家ではクロム – Zigu

+1

クロムでCtrl-Shift-iを押してください。 –

答えて

2

あなたget_all_users()機能を備えた唯一のUID 1-18がありますが、その後json_encodingされている、あなたの$output_stringにHTMLを追加します。それはJSONの仕組みではありません。つまり、データベース呼び出しはコントローラではなくモデルで実行する必要があります。

+0

どのようにajaxでモデルを呼び出すのですか?チュートリアルを見て、彼らは完全なコントローラのメソッドのURLを使用するので、私はそれを適用可能なものに変換する方法がわかりません。 – Zigu

+0

簡単に言えば、コントローラはURLを処理し、どのようにデータが返されるかを決定します。モデルは、ユーザーなどのデータとのやりとりを制御します。テーブル内のデータをラップすることは、あなたのコントローラで、より理想的には、あなたのビュー内で起こるはずです - モデルでは決してありません。CIのドキュメンテーションとMVCの仕組みをもう一度見てください:http://codeigniter.com/user_guide/overview/ mvc.html – Seabass

関連する問題