Advertisement
joris

Load Data From API to Combo

Oct 10th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. API di Yii :
  2. =============
  3. public function actionView()
  4. {
  5.     $sql = "SELECT * from tbl_data";
  6.     $connection=Yii::app()->db;
  7.     $command=$connection->createCommand($sql);
  8.     $dataReader=$command->queryAll();
  9.     echo CJSON::encode($dataReader);
  10. }
  11.  
  12. ======================================================
  13.  
  14. Di jQuery :
  15. ============
  16.     $.ajax({
  17.         type: 'GET',
  18.         url: '<?php echo Yii::app()->createAbsoluteUrl("api/view"); ?>',
  19.         dataType: 'json',
  20.         cache: false,
  21.         success: function (result) {
  22.             $.each(result, function() {
  23.                 $("#idCombonya").append($('<option>', {
  24.                     value: this.strkey,
  25.                     text : this.strvalue
  26.                 }));
  27.             });
  28.         },
  29.         error: function(jqXHR, exception){
  30.             if (jqXHR.status === 0) {
  31.                 alert('Not connect.\n Verify Network.');
  32.             } else if (jqXHR.status == 404) {
  33.                 alert('Requested page not found. [404]');
  34.             } else if (jqXHR.status == 500) {
  35.                 alert('Internal Server Error [500]');
  36.             } else if (exception === 'parsererror') {
  37.                 alert('Requested JSON parse failed.')
  38.             } else if (exception === 'timeout') {
  39.                 alert('Time out error.')
  40.             } else if (exception === 'abort') {
  41.                 alert('Ajax request aborted.')
  42.             } else {
  43.                 alert('Uncaught Error.\n' + jqXHR.responseText);
  44.             }
  45.         }
  46.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement