2016-07-15 9 views
1

私はcodeigniterの新機能です。jquery ajaxレスポンスデータを取得し、codeigniterビューで表示する方法html div id

ウィンドウロード時にajaxからcodeigniterビューのdiv idにデータを表示したいとします。

View.php

<script type='text/javascript' language='javascript'> 
$(window).load(function() 
{ 
    $.ajax({ 
     type: "POST", 
     url: "<?php echo base_url(); ?>colleges/index",  
     success: function(server_response 
     { 
      if(server_response == 'success') 
      { 
       $("#fillgrid").html(server_response); 
      } 
      else{ 
       alert('Not OKay'); 
      } 
         } 
    }); //$.ajax ends here 
}); 
</script> 
+1

ここでの問題は何ですか?あなたはどこかで立ち往生していますか? –

答えて

0

私は、コードに直接説明を追加しました:簡体

$(window).load(function() 
{ 
    $.ajax({ 
     type: "POST", 
     url: "<?php echo base_url(); ?>colleges/index",  
     success: function(server_response)   // Added missing parenthesis 
     { 
      if(server_response == 'success')   // You already are in the success callback. 
      {           
       $("#fillgrid").html(server_response); 
      } 
      else{ 
       alert('Not OKay'); 
      } 
         } 
    }); //$.ajax ends here 
}); 

機能:

$.ajax({ 
    type: "POST", 
    url: "<?php echo base_url(); ?>colleges/index",  
    success: function(server_response){ 
     $("#fillgrid").html(server_response); 
    }, 
    error: function(){ 
     alert('Not Okay'); 
    } 
}); //$.ajax ends here 

ドン」​​メソッド内にAjax関数を配置してください。
.loadは、.ajax()のjQueryの略語です。
だからどちらかというと...両方ではありません。

​​を使用して、それは次のようになります。

$("#fillgrid").load("<?php echo base_url(); ?>colleges/index"); 
+0

私のmodel.php 公開関数get_all_college() { $ this-> db-> order_by( "id"、 "DESC"); $ query = $ this-> db-> get( "tbl_colleges"); return $ query-> result_array();私controller.php パブリック関数インデックス(){ $リスト[ '大学'] =の$ this - > Colleges_model-> get_all_college()において }。 $ this-> load-> view( 'header'); $ this-> load-> view( 'Colleges/all_colleges'、$ list); $ this-> load-> view( 'footer'); } コードの上に表示されます。 –

+0

Ajax 'server_response'には生成されたhtmlが含まれています。私は実際にモデル/コントローラ/ビューフレームワークに慣れていません...あなたは結果をエコーするために何かを作らなければなりません。 –

+0

«コードの真上にあります»PHPスクリプトとjQueryスクリプトとの関係はありません。 PHPはサーバ側を実行... jQueryはPHPの後でクライアント側で「長い」実行されます。 ;)ここで、PHPを再度呼び出すページを生成します...私はあなたが一般的なエラーを起こさないことを願っています:自分自身を呼び出すページ...コンテンツと呼ばれるajaxは、mainページ。 –

0

はこれを試してみてください:

$(document).ready(function(){ 
     $.ajax({ 
      url: "<?php echo base_url(); ?>colleges/index", 
      context: document.body, 
      type: 'POST'; 
      success: function(result) 
     { 
      if(result) { 
       alert("success"); 
      } else { 
       alert("error"); 
      } 
     } 
     }); 
    }); 
関連する問題